Categorygithub.com/Azure/azure-sdk-for-go/sdk/messaging/azwebpubsub
modulepackage
0.1.0
Repository: https://github.com/azure/azure-sdk-for-go.git
Documentation: pkg.go.dev

# 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:

overflow.

  • 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:

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.

# Functions

NewClient creates a client that manages Web PubSub service.
NewClientFromConnectionString creates a Client from a connection string.
PossibleContentTypeValues returns the possible values for the ContentType const type.
PossiblePermissionValues returns the possible values for the Permission const type.

# Constants

ContentTypeApplicationJSON - Content Type 'application/json'.
ContentTypeApplicationOctetStream - Content Type 'application/octet-stream'.
ContentTypeTextPlain - Content Type 'text/plain'.
No description provided by the author
No description provided by the author

# Structs

AddConnectionsToGroupsOptions contains the optional parameters for the Client.AddConnectionsToGroups method.
AddConnectionsToGroupsResponse contains the response from method Client.AddConnectionsToGroups.
AddConnectionToGroupOptions contains the optional parameters for the Client.AddConnectionToGroup method.
AddConnectionToGroupResponse contains the response from method Client.AddConnectionToGroup.
AddToGroupsRequest - The request object containing targets groups and a connection filter.
AddUserToGroupOptions contains the optional parameters for the Client.AddUserToGroup method.
AddUserToGroupResponse contains the response from method Client.AddUserToGroup.
CheckPermissionOptions contains the optional parameters for the Client.CheckPermission method.
CheckPermissionResponse contains the response from method Client.CheckPermission.
Client contains the methods for the WebPubSub group.
ClientOptions contains optional settings for [Client].
CloseAllConnectionsOptions contains the optional parameters for the Client.CloseAllConnections method.
CloseAllConnectionsResponse contains the response from method Client.CloseAllConnections.
CloseConnectionOptions contains the optional parameters for the Client.CloseConnection method.
CloseConnectionResponse contains the response from method Client.CloseConnection.
CloseGroupConnectionsOptions contains the optional parameters for the Client.CloseGroupConnections method.
CloseGroupConnectionsResponse contains the response from method Client.CloseGroupConnections.
CloseUserConnectionsOptions contains the optional parameters for the Client.CloseUserConnections method.
CloseUserConnectionsResponse contains the response from method Client.CloseUserConnections.
ConnectionExistsOptions contains the optional parameters for the Client.ConnectionExists method.
ConnectionExistsResponse contains the response from method Client.ConnectionExists.
GenerateClientAccessURLOptions represents the options for generating a client access url.
GenerateClientAccessURLResponse represents the response type for the generated client access url.
GenerateClientTokenOptions contains the optional parameters for the Client.GenerateClientToken method.
GenerateClientTokenResponse contains the response from method Client.GenerateClientToken.
GrantPermissionOptions contains the optional parameters for the Client.GrantPermission method.
GrantPermissionResponse contains the response from method Client.GrantPermission.
GroupExistsOptions contains the optional parameters for the Client.GroupExists method.
GroupExistsResponse contains the response from method Client.GroupExists.
RemoveConnectionFromAllGroupsOptions contains the optional parameters for the Client.RemoveConnectionFromAllGroups method.
RemoveConnectionFromAllGroupsResponse contains the response from method Client.RemoveConnectionFromAllGroups.
RemoveConnectionFromGroupOptions contains the optional parameters for the Client.RemoveConnectionFromGroup method.
RemoveConnectionFromGroupResponse contains the response from method Client.RemoveConnectionFromGroup.
RemoveConnectionsFromGroupsOptions contains the optional parameters for the Client.RemoveConnectionsFromGroups method.
RemoveConnectionsFromGroupsResponse contains the response from method Client.RemoveConnectionsFromGroups.
RemoveFromGroupsRequest - The request object containing targets groups and a connection filter.
RemoveUserFromAllGroupsOptions contains the optional parameters for the Client.RemoveUserFromAllGroups method.
RemoveUserFromAllGroupsResponse contains the response from method Client.RemoveUserFromAllGroups.
RemoveUserFromGroupOptions contains the optional parameters for the Client.RemoveUserFromGroup method.
RemoveUserFromGroupResponse contains the response from method Client.RemoveUserFromGroup.
RevokePermissionOptions contains the optional parameters for the Client.RevokePermission method.
RevokePermissionResponse contains the response from method Client.RevokePermission.
SendToAllOptions contains the optional parameters for the Client.SendToAll method.
SendToAllResponse contains the response from method Client.SendToAll.
SendToConnectionOptions contains the optional parameters for the Client.SendToConnection method.
SendToConnectionResponse contains the response from method Client.SendToConnection.
SendToGroupOptions contains the optional parameters for the Client.SendToGroup method.
SendToGroupResponse contains the response from method Client.SendToGroup.
SendToUserOptions contains the optional parameters for the Client.SendToUser method.
SendToUserResponse contains the response from method Client.SendToUser.
TokenResponse - The response object containing the token for the client.
UserExistsOptions contains the optional parameters for the Client.UserExists method.
UserExistsResponse contains the response from method Client.UserExists.

# Type aliases

ContentType - Content type for upload.
Permission contains the allowed permissions.