Share via


WebPubSubServiceClient class

Client for connecting to a Web PubSub hub

Constructors

WebPubSubServiceClient(string, AzureKeyCredential | TokenCredential, string, WebPubSubServiceClientOptions)

Creates an instance of a WebPubSubServiceClient for sending messages and managing groups, connections, and users.

Example usage:

import { AzureKeyCredential, WebPubSubServiceClient } from "@azure/web-pubsub";

const key = new AzureKeyCredential("<Key>");
const serviceClient = new WebPubSubServiceClient("<Endpoint>", key, "<hubName>");
WebPubSubServiceClient(string, string, WebPubSubServiceClientOptions)

Creates an instance of a WebPubSubServiceClient for sending messages and managing groups, connections, and users.

Example usage:

import { WebPubSubServiceClient } from "@azure/web-pubsub";

const serviceClient = new WebPubSubServiceClient("<ConnectionString>", "<hubName>");

Properties

apiVersion

The Web PubSub API version being used by this client

endpoint

The Web PubSub endpoint this client is connected to

hubName

The name of the hub this client is connected to

Methods

addConnectionsToGroups(string[], string, GroupAddConnectionOptions)

Add filtered connections to multiple groups

closeAllConnections(HubCloseAllConnectionsOptions)

Close all connections to this hub

closeConnection(string, HubCloseConnectionOptions)

Close a specific connection to this hub

closeUserConnections(string, HubCloseUserConnectionsOptions)

Close all connections with the given user id

connectionExists(string, HasConnectionOptions)

Check if a specific connection is connected to this hub

getClientAccessToken(GenerateClientTokenOptions)

Generate a token for a client to connect to the Azure Web PubSub service.

grantPermission(string, Permission, HubGrantPermissionOptions)

Grant permissions to a connection

group(string)

Get a client for a group

groupExists(string, HubHasGroupOptions)

Check if a particular group exists (i.e. has active connections).

hasPermission(string, Permission, HubHasPermissionOptions)

Check if the connection has the specified permission

removeConnectionFromAllGroups(string, HubCloseConnectionOptions)

Remove a specific connection from all groups they are joined to

removeConnectionsFromGroups(string[], string, GroupRemoveConnectionOptions)

Remove filtered connections from multiple groups

removeUserFromAllGroups(string, HubCloseConnectionOptions)

Remove a specific user from all groups they are joined to

revokePermission(string, Permission, HubRevokePermissionOptions)

Revoke permissions from a connection

sendToAll(JSONTypes, HubSendToAllOptions)

Broadcast a JSON message to all connections on this hub.

sendToAll(RequestBodyType, HubSendToAllOptions)

Broadcast a binary message to all connections on this hub.

sendToAll(string, HubSendTextToAllOptions)

Broadcast a text message to all connections on this hub.

sendToConnection(string, JSONTypes, HubSendToConnectionOptions)

Send a binary message to a specific connection

sendToConnection(string, RequestBodyType, HubSendTextToConnectionOptions | HubSendToConnectionOptions)

Send a binary message to a specific connection

sendToConnection(string, string, HubSendTextToConnectionOptions)

Send a text message to a specific connection

sendToUser(string, JSONTypes, HubSendToUserOptions)

Send a JSON message to a specific user

sendToUser(string, RequestBodyType, HubSendTextToUserOptions | HubSendToUserOptions)

Send a binary message to a specific user

sendToUser(string, string, HubSendTextToUserOptions)

Send a text message to a specific user

userExists(string, HubHasUserOptions)

Check if a particular user is connected to this hub.

Constructor Details

WebPubSubServiceClient(string, AzureKeyCredential | TokenCredential, string, WebPubSubServiceClientOptions)

Creates an instance of a WebPubSubServiceClient for sending messages and managing groups, connections, and users.

Example usage:

import { AzureKeyCredential, WebPubSubServiceClient } from "@azure/web-pubsub";

const key = new AzureKeyCredential("<Key>");
const serviceClient = new WebPubSubServiceClient("<Endpoint>", key, "<hubName>");
new WebPubSubServiceClient(endpoint: string, credential: AzureKeyCredential | TokenCredential, hubName: string, options?: WebPubSubServiceClientOptions)

Parameters

endpoint

string

The endpoint to connect to

credential

AzureKeyCredential | TokenCredential

An AzureKeyCredential holding your service key

hubName

string

The name of the hub to connect to.

options
WebPubSubServiceClientOptions

Options to configure the http pipeline

WebPubSubServiceClient(string, string, WebPubSubServiceClientOptions)

Creates an instance of a WebPubSubServiceClient for sending messages and managing groups, connections, and users.

Example usage:

import { WebPubSubServiceClient } from "@azure/web-pubsub";

const serviceClient = new WebPubSubServiceClient("<ConnectionString>", "<hubName>");
new WebPubSubServiceClient(connectionString: string, hubName: string, options?: WebPubSubServiceClientOptions)

Parameters

connectionString

string

The connection string

hubName

string

The name of the hub to connect to. If omitted, '_default' is used.

options
WebPubSubServiceClientOptions

Options to configure the http pipeline

Property Details

apiVersion

The Web PubSub API version being used by this client

apiVersion: string

Property Value

string

endpoint

The Web PubSub endpoint this client is connected to

endpoint: string

Property Value

string

hubName

The name of the hub this client is connected to

hubName: string

Property Value

string

Method Details

addConnectionsToGroups(string[], string, GroupAddConnectionOptions)

Add filtered connections to multiple groups

function addConnectionsToGroups(groups: string[], filter: string, options?: GroupAddConnectionOptions): Promise<void>

Parameters

groups

string[]

A list of groups which target connections will be added into

filter

string

An OData filter which target connections satisfy

options
GroupAddConnectionOptions

Additional options

Returns

Promise<void>

closeAllConnections(HubCloseAllConnectionsOptions)

Close all connections to this hub

function closeAllConnections(options?: HubCloseAllConnectionsOptions): Promise<void>

Parameters

options
HubCloseAllConnectionsOptions

Additional options

Returns

Promise<void>

closeConnection(string, HubCloseConnectionOptions)

Close a specific connection to this hub

function closeConnection(connectionId: string, options?: HubCloseConnectionOptions): Promise<void>

Parameters

connectionId

string

Connection id to close

options
HubCloseConnectionOptions

Additional options

Returns

Promise<void>

closeUserConnections(string, HubCloseUserConnectionsOptions)

Close all connections with the given user id

function closeUserConnections(userId: string, options?: HubCloseUserConnectionsOptions): Promise<void>

Parameters

userId

string

options
HubCloseUserConnectionsOptions

Additional options

Returns

Promise<void>

connectionExists(string, HasConnectionOptions)

Check if a specific connection is connected to this hub

function connectionExists(connectionId: string, options?: HasConnectionOptions): Promise<boolean>

Parameters

connectionId

string

Connection id to check

options
HasConnectionOptions

Additional options

Returns

Promise<boolean>

getClientAccessToken(GenerateClientTokenOptions)

Generate a token for a client to connect to the Azure Web PubSub service.

function getClientAccessToken(options?: GenerateClientTokenOptions): Promise<ClientTokenResponse>

Parameters

options
GenerateClientTokenOptions

Additional options

Returns

grantPermission(string, Permission, HubGrantPermissionOptions)

Grant permissions to a connection

function grantPermission(connectionId: string, permission: Permission, options?: HubGrantPermissionOptions): Promise<void>

Parameters

connectionId

string

The connection id to grant permissions to

permission
Permission
options
HubGrantPermissionOptions

Additional options

Returns

Promise<void>

group(string)

Get a client for a group

function group(groupName: string): WebPubSubGroup

Parameters

groupName

string

The name of the group to connect to.

Returns

groupExists(string, HubHasGroupOptions)

Check if a particular group exists (i.e. has active connections).

function groupExists(groupName: string, options?: HubHasGroupOptions): Promise<boolean>

Parameters

groupName

string

The group name to check for

options
HubHasGroupOptions

Additional options

Returns

Promise<boolean>

hasPermission(string, Permission, HubHasPermissionOptions)

Check if the connection has the specified permission

function hasPermission(connectionId: string, permission: Permission, options?: HubHasPermissionOptions): Promise<boolean>

Parameters

connectionId

string

The connection id to check permission

permission
Permission
options
HubHasPermissionOptions

Additional options

Returns

Promise<boolean>

removeConnectionFromAllGroups(string, HubCloseConnectionOptions)

Remove a specific connection from all groups they are joined to

function removeConnectionFromAllGroups(connectionId: string, options?: HubCloseConnectionOptions): Promise<void>

Parameters

connectionId

string

The connection id to remove from all groups

options
HubCloseConnectionOptions

Additional options

Returns

Promise<void>

removeConnectionsFromGroups(string[], string, GroupRemoveConnectionOptions)

Remove filtered connections from multiple groups

function removeConnectionsFromGroups(groups: string[], filter: string, options?: GroupRemoveConnectionOptions): Promise<void>

Parameters

groups

string[]

A list of groups which target connections will be removed from

filter

string

An OData filter which target connections satisfy

options
GroupRemoveConnectionOptions

Additional options

Returns

Promise<void>

removeUserFromAllGroups(string, HubCloseConnectionOptions)

Remove a specific user from all groups they are joined to

function removeUserFromAllGroups(userId: string, options?: HubCloseConnectionOptions): Promise<void>

Parameters

userId

string

The user id to remove from all groups

options
HubCloseConnectionOptions

Additional options

Returns

Promise<void>

revokePermission(string, Permission, HubRevokePermissionOptions)

Revoke permissions from a connection

function revokePermission(connectionId: string, permission: Permission, options?: HubRevokePermissionOptions): Promise<void>

Parameters

connectionId

string

The connection id to revoke permissions from

permission
Permission
options
HubRevokePermissionOptions

Additional options

Returns

Promise<void>

sendToAll(JSONTypes, HubSendToAllOptions)

Broadcast a JSON message to all connections on this hub.

function sendToAll(message: JSONTypes, options?: HubSendToAllOptions): Promise<void>

Parameters

message
JSONTypes

The JSON message to send

options
HubSendToAllOptions

Additional options

Returns

Promise<void>

sendToAll(RequestBodyType, HubSendToAllOptions)

Broadcast a binary message to all connections on this hub.

function sendToAll(message: RequestBodyType, options?: HubSendToAllOptions): Promise<void>

Parameters

message
RequestBodyType

The message to send

options
HubSendToAllOptions

Additional options

Returns

Promise<void>

sendToAll(string, HubSendTextToAllOptions)

Broadcast a text message to all connections on this hub.

function sendToAll(message: string, options: HubSendTextToAllOptions): Promise<void>

Parameters

message

string

The text message to send

options
HubSendTextToAllOptions

Additional options

Returns

Promise<void>

sendToConnection(string, JSONTypes, HubSendToConnectionOptions)

Send a binary message to a specific connection

function sendToConnection(connectionId: string, message: JSONTypes, options?: HubSendToConnectionOptions): Promise<void>

Parameters

connectionId

string

Connection id to send to

message
JSONTypes

The JSON message

options
HubSendToConnectionOptions

Additional options

Returns

Promise<void>

sendToConnection(string, RequestBodyType, HubSendTextToConnectionOptions | HubSendToConnectionOptions)

Send a binary message to a specific connection

function sendToConnection(connectionId: string, message: RequestBodyType, options?: HubSendTextToConnectionOptions | HubSendToConnectionOptions): Promise<void>

Parameters

connectionId

string

Connection id to send to

message
RequestBodyType

The binary message

Returns

Promise<void>

sendToConnection(string, string, HubSendTextToConnectionOptions)

Send a text message to a specific connection

function sendToConnection(connectionId: string, message: string, options: HubSendTextToConnectionOptions): Promise<void>

Parameters

connectionId

string

Connection id to send to

message

string

The text message

options
HubSendTextToConnectionOptions

Additional options

Returns

Promise<void>

sendToUser(string, JSONTypes, HubSendToUserOptions)

Send a JSON message to a specific user

function sendToUser(username: string, message: JSONTypes, options?: HubSendToUserOptions): Promise<void>

Parameters

username

string

User name to send to

message
JSONTypes

The josn message to send

options
HubSendToUserOptions

Additional options

Returns

Promise<void>

sendToUser(string, RequestBodyType, HubSendTextToUserOptions | HubSendToUserOptions)

Send a binary message to a specific user

function sendToUser(username: string, message: RequestBodyType, options?: HubSendTextToUserOptions | HubSendToUserOptions): Promise<void>

Parameters

username

string

The user name to send to

message
RequestBodyType

The binary message to send

options

HubSendTextToUserOptions | HubSendToUserOptions

Additional options

Returns

Promise<void>

sendToUser(string, string, HubSendTextToUserOptions)

Send a text message to a specific user

function sendToUser(username: string, message: string, options: HubSendTextToUserOptions): Promise<void>

Parameters

username

string

User name to send to

message

string

The text message to send

options
HubSendTextToUserOptions

Additional options

Returns

Promise<void>

userExists(string, HubHasUserOptions)

Check if a particular user is connected to this hub.

function userExists(username: string, options?: HubHasUserOptions): Promise<boolean>

Parameters

username

string

The user name to check for

options
HubHasUserOptions

Additional options

Returns

Promise<boolean>