@azure/web-pubsub-client v1.0.1
Azure Web PubSub Client
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 client side to manage the WebSocket client connections, as shown in below diagram:
Details about the terms used here are described in Key concepts section.
API reference documentation | Product documentation | Samples
Getting started
Currently supported environments
Prerequisites
- An Azure subscription.
- An existing Azure Web PubSub endpoint.
1. Install the @azure/web-pubsub-client
package
npm install @azure/web-pubsub-client
2. Create a WebPubSubClient
and copy client-access-url
from Azure Portal
const { WebPubSubClient } = require("@azure/web-pubsub-client");
client = new WebPubSubClient("<<client-access-url>>");
await client.start();
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.
Client Events
Events are created during the lifecycle of a client connection. For example, a simple WebSocket client connection creates a connect
event when it tries to connect to the service, a connected
event when it successfully connected to the service, a message
event when it sends messages to the service and a disconnected
event when it disconnects from the service.
Event Handler
Event handler contains the logic to handle the client events. Event handler needs to be registered and configured in the service through the portal or Azure CLI beforehand. The place to host the event handler logic is generally considered as the server-side.
Examples
Start a client
const { WebPubSubClient } = require("@azure/web-pubsub-client");
client = new WebPubSubClient("<<client-access-url>>");
await client.start();
Troubleshooting
Enable logs
You can set the following environment variable to get the debug logs when using this library.
- Getting debug logs from the SignalR client library
export AZURE_LOG_LEVEL=verbose
For more detailed instructions on how to enable logs, you can look at the @azure/logger package docs.
Live Trace
Use Live Trace from the Web PubSub service portal to view the live traffic.
Next steps
Please take a look at the samples directory for detailed examples on how to use this library.
Contributing
If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.
Related projects
20 hours ago
2 days ago
5 days ago
6 days ago
7 days ago
8 days ago
8 days ago
9 days ago
12 days ago
13 days ago
14 days ago
15 days ago
16 days ago
19 days ago
20 days ago
21 days ago
22 days ago
28 days ago
29 days ago
1 month ago
2 months ago
2 months ago
3 months ago
3 months ago
4 months ago
4 months ago
4 months ago
4 months ago
5 months ago
5 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
9 months ago
7 months ago
8 months ago
6 months ago
8 months ago
7 months ago
7 months ago
7 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago