# README
Azure Web PubSub service client library for Go
Azure Web PubSub service is an Azure-managed service that helps developers easily build web applications with real-time features and publish-subscribe pattern. Any scenario that requires real-time publish-subscribe messaging between server and clients or among clients can use Azure Web PubSub service. Traditional real-time features that often require polling from server or submitting HTTP requests can also use Azure Web PubSub service.
You can use this library in your app server side to manage the WebSocket client connections, as shown in below diagram:
.
- Send messages to hubs and groups.
- Send messages to particular users and connections.
- Organize users and connections into groups.
- Close connections
- Grant, revoke, and check permissions for an existing connection
Details about the terms used here are described in Key concepts section.
Key links:
Getting started
Install the package
Install the Azure Web PubSub service client module for Go with go get
:
go get github.com/Azure/azure-sdk-for-go/sdk/messaging/azwebpubsub
Prerequisites
- Go, version 1.18 or higher
- An Azure subscription
- An existing Azure Web PubSub service instance.
Authenticate the client
Web PubSub service clients are created using a TokenCredential from the Azure Identity package, like DefaultAzureCredential. You can also create a client using a connection string.
Using a service principal
Constructing the client requires your Web PubSub's endpoint URL, which you can get from the Azure Portal (Host name
value on overview page with https
scheme).
import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azwebpubsub"
"log"
)
func main() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
client, err := azwebpubsub.NewClient("<your Web PubSub's endpoint URL>", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
}
Using a connection string
ConnectionString can be found in the Keys tab from your Web PubSub resource portal.
import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azwebpubsub"
"log"
)
func main() {
client, err := azwebpubsub.NewClientFromConnectionString("<your Web PubSub's connection string>", nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
}
Key concepts
Connection
A connection, also known as a client or a client connection, represents an individual WebSocket connection connected to the Web PubSub service. When successfully connected, a unique connection ID is assigned to this connection by the Web PubSub service.
Hub
A hub is a logical concept for a set of client connections. Usually you use one hub for one purpose, for example, a chat hub, or a notification hub. When a client connection is created, it connects to a hub, and during its lifetime, it belongs to that hub. Different applications can share one Azure Web PubSub service by using different hub names.
Group
A group is a subset of connections to the hub. You can add a client connection to a group, or remove the client connection from the group, anytime you want. For example, when a client joins a chat room, or when a client leaves the chat room, this chat room can be considered to be a group. A client can join multiple groups, and a group can contain multiple clients.
User
Connections to Web PubSub can belong to one user. A user might have multiple connections, for example when a single user is connected across multiple devices or multiple browser tabs.
Message
When the client is connected, it can send messages to the upstream application, or receive messages from the upstream application, through the WebSocket connection.
Examples
Examples for various scenarios can be found on pkg.go.dev or in the example*_test.go files in our GitHub repo for azwebpubsub.
Troubleshooting
Live Trace
Use Live Trace from the Web PubSub service portal to view the live traffic.
Logging
This module uses the classification-based logging implementation in azcore
. To enable console logging for all SDK modules, set the environment variable AZURE_SDK_GO_LOGGING
to all
.
Use the azcore/log
package to control log event output or to enable logs for azwebpubsub
only. For example:
import (
"fmt"
azlog "github.com/Azure/azure-sdk-for-go/sdk/azcore/log"
)
// print log output to stdout
azlog.SetListener(func(event azlog.Event, s string) {
fmt.Printf("[%s] %s\n", event, s)
})
// pick the set of events to log
azlog.SetEvents(
azwebpubsub
)
Contributing
For details on contributing to this repository, see the contributing guide.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
Additional Helpful Links for Contributors
Many people all over the world have helped make this project better. You'll want to check out:
- What are some good first issues for new contributors to the repo?
- How to build and test your change
- How you can make a change happen!
- Frequently Asked Questions (FAQ) and Conceptual Topics in the detailed Azure SDK for Go wiki.
Reporting security issues and security bugs
Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) [email protected]. You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the MSRC PGP key, can be found in the Security TechCenter.
License
Azure SDK for Go is licensed under the MIT license.