WebPubSubClient Class

Constructor

WebPubSubClient(credential: WebPubSubClientCredential | str, *, message_retry_backoff_factor: float = 0.8, message_retry_backoff_max: float = 120.0, message_retry_mode: RetryMode = RetryMode.Exponential, message_retry_total: int = 3, protocol_type: WebPubSubProtocolType = WebPubSubProtocolType.JSON_RELIABLE, reconnect_retry_backoff_factor: float = 0.8, reconnect_retry_backoff_max: float = 120.0, reconnect_retry_mode: RetryMode = RetryMode.Exponential, reconnect_retry_total: int = 3, auto_rejoin_groups: bool = True, logging_enable: bool = False, ack_timeout: float = 30.0, start_timeout: float = 30.0, user_agent: str | None = None, **kwargs: Any)

Parameters

Name Description
credential
Required

The url to connect or credential to use when connecting. Required.

Keyword-Only Parameters

Name Description
auto_rejoin_groups

Whether to enable restoring group after reconnecting

Default value: True
protocol_type

Subprotocol type

Default value: WebPubSubProtocolType.JSON_RELIABLE
reconnect_retry_total
int

total number of retries to allow for reconnect. If 0, it means disable reconnect. Default is 3.

Default value: 3
reconnect_retry_backoff_factor

A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a second try without a delay). In fixed mode, retry policy will always sleep for {backoff factor}. In 'exponential' mode, retry policy will sleep for: "{backoff factor} * (2 ** ({number of retries} - 1))" seconds. If the backoff_factor is 0.1, then the retry will sleep for [0.0s, 0.2s, 0.4s, ...] between retries. The default value is 0.8.

Default value: 0.8
reconnect_retry_backoff_max

The maximum back off time. Default value is 120.0 seconds

Default value: 120.0
reconnect_retry_mode
<xref:azure.messaging.webpubsubclient.RetryMode>

Fixed or exponential delay between attemps, default is exponential.

Default value: RetryMode.Exponential
message_retry_total
int

total number of retries to allow for sending message. Default is 3.

Default value: 3
message_retry_backoff_factor

A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a second try without a delay). In fixed mode, retry policy will always sleep for {backoff factor}. In 'exponential' mode, retry policy will sleep for: "{backoff factor} * (2 ** ({number of retries} - 1))" seconds. If the backoff_factor is 0.1, then the retry will sleep for [0.0s, 0.2s, 0.4s, ...] between retries. The default value is 0.8.

Default value: 0.8
message_retry_backoff_max

The maximum back off time. Default value is 120.0 seconds

Default value: 120.0
message_retry_mode
<xref:RetryMode>

Fixed or exponential delay between attemps, default is exponential.

Default value: RetryMode.Exponential
auto_rejoin_groups

auto_rejoin_groups, default is True

logging_enable

Whether to output network trace logs to the logger. Default is False.

Default value: False
ack_timeout

Time limit to wait for ack message from server. The default value is 30.0 seconds.

Default value: 30.0
start_timeout

Time limit to wait for successful client open. The default value is 30.0 seconds.

Default value: 30.0
user_agent
str

The user agent to be used for the request. If specified, this will be added in front of the default user agent string.

Default value: None

Methods

close

close the client

is_connected

check whether the client is still connected to server after open

join_group

Join the client to group.

leave_group

Leave the client from group :param group_name: The group name. Required. :type group_name: str. :keyword int ack_id: The optional ackId. If not specified, client will generate one.

open

open the client and connect to service

send_event

Send custom event to server. For more info about event handler in web pubsub, please refer to https://learn.microsoft.com/en-us/azure/azure-web-pubsub/howto-develop-eventhandler

send_to_group

Send message to group. :param group_name: The group name. Required. :type group_name: str. :param content: The data content. Required. :type content: Union[str, memoryview, Dict[str, Any]]. :param data_type: The data type. Required. :type data_type: ~azure.messaging.webpubsubclient.models.WebPubSubDataType or str. :keyword int ack_id: The optional ackId. If not specified, client will generate one. :keyword bool ack: If False, the message won't contains ackId and no AckMessage

will be returned from the service. Default is True.

subscribe

Add handler. :param event: The event name. Required. :type event: ~azure.messaging.webpubsubclient.models.CallbackType :param listener: The handler :type listener: callable.

unsubscribe

Remove handler for rejoining group failed. :param event: The event name. Required. :type event: ~azure.messaging.webpubsubclient.models.CallbackType :param listener: The handler :type listener: callable.

close

close the client

close() -> None

is_connected

check whether the client is still connected to server after open

is_connected() -> bool

Returns

Type Description

True if the client is connected to server, otherwise False

join_group

Join the client to group.

join_group(group_name: str, **kwargs: Any) -> None

Parameters

Name Description
group_name
Required
<xref:str.>

The group name. Required.

Keyword-Only Parameters

Name Description
ack_id
int

The optional ackId. If not specified, client will generate one.

leave_group

Leave the client from group :param group_name: The group name. Required. :type group_name: str. :keyword int ack_id: The optional ackId. If not specified, client will generate one.

leave_group(group_name: str, **kwargs: Any) -> None

Parameters

Name Description
group_name
Required

open

open the client and connect to service

open() -> None

send_event

Send custom event to server. For more info about event handler in web pubsub, please refer to https://learn.microsoft.com/en-us/azure/azure-web-pubsub/howto-develop-eventhandler

send_event(event_name: str, content: str, data_type: Literal[WebPubSubDataType.TEXT], **kwargs: Any) -> None

Parameters

Name Description
event_name
Required
<xref:str.>

The event name. Required.

content
Required
Union[str, memoryview, Dict[str, Any]]<xref:./>

The data content that you want to send to event handler that registered in web pubsub. Required.

data_type
Required

The data type. Required.

Keyword-Only Parameters

Name Description
ack_id
int

The optional ackId. If not specified, client will generate one.

ack

If False, the message won't contains ackId and no AckMessage will be returned from the service. Default is True.

send_to_group

Send message to group. :param group_name: The group name. Required. :type group_name: str. :param content: The data content. Required. :type content: Union[str, memoryview, Dict[str, Any]]. :param data_type: The data type. Required. :type data_type: ~azure.messaging.webpubsubclient.models.WebPubSubDataType or str. :keyword int ack_id: The optional ackId. If not specified, client will generate one. :keyword bool ack: If False, the message won't contains ackId and no AckMessage

will be returned from the service. Default is True.

send_to_group(group_name: str, content: str, data_type: Literal[WebPubSubDataType.TEXT], **kwargs: Any) -> None

Parameters

Name Description
group_name
Required
content
Required
data_type
Required

Keyword-Only Parameters

Name Description
no_echo

Whether the message needs to echo to sender. Default is False.

subscribe

Add handler. :param event: The event name. Required. :type event: ~azure.messaging.webpubsubclient.models.CallbackType :param listener: The handler :type listener: callable.

subscribe(event: Literal[CallbackType.CONNECTED], listener: Callable[[OnConnectedArgs], None]) -> None

Parameters

Name Description
event
Required
listener
Required

unsubscribe

Remove handler for rejoining group failed. :param event: The event name. Required. :type event: ~azure.messaging.webpubsubclient.models.CallbackType :param listener: The handler :type listener: callable.

unsubscribe(event: Literal[CallbackType.CONNECTED], listener: Callable[[OnConnectedArgs], None]) -> None

Parameters

Name Description
event
Required
listener
Required