azure.messaging.webpubsubservice package

class azure.messaging.webpubsubservice.WebPubSubServiceClient(endpoint: str, credential: Union[TokenCredential, AzureKeyCredential], **kwargs: Any)[source]

WebPubSubServiceClient.

Parameters
  • endpoint (str) – HTTP or HTTPS endpoint for the Web PubSub service instance.

  • credential (TokenCredential) – Credential needed for the client to connect to Azure.

add_connection_to_group(hub: str, group: str, connection_id: str, **kwargs: Any)None

Add a connection to the target group.

Add a connection to the target group.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • group (str) – Target group name, which length should be greater than 0 and less than 1025.

  • connection_id (str) – Target connection Id.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

add_user_to_group(hub: str, group: str, user_id: str, **kwargs: Any)None

Add a user to the target group.

Add a user to the target group.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • group (str) – Target group name, which length should be greater than 0 and less than 1025.

  • user_id (str) – Target user Id.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

check_permission(hub: str, permission: str, connection_id: str, **kwargs: Any)None

Check if a connection has permission to the specified action.

Check if a connection has permission to the specified action.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • permission (str) – The permission: current supported actions are joinLeaveGroup and sendToGroup. Possible values are: “sendToGroup” or “joinLeaveGroup”.

  • connection_id (str) – Target connection Id.

Keyword Arguments

target_name (str) – The meaning of the target depends on the specific permission. For joinLeaveGroup and sendToGroup, targetName is a required parameter standing for the group name.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

close()None[source]
close_all_connections(hub: str, **kwargs: Any)None

Close the connections in the hub.

Close the connections in the hub.

Parameters

hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

Keyword Arguments
  • excluded (list[str]) – Exclude these connectionIds when closing the connections in the hub.

  • reason (str) – The reason closing the client connection.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

close_connection(hub: str, connection_id: str, **kwargs: Any)None

Close the client connection.

Close the client connection.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • connection_id (str) – Target connection Id.

Keyword Arguments

reason (str) – The reason closing the client connection.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

close_group_connections(hub: str, group: str, **kwargs: Any)None

Close connections in the specific group.

Close connections in the specific group.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • group (str) – Target group name, which length should be greater than 0 and less than 1025.

Keyword Arguments
  • excluded (list[str]) – Exclude these connectionIds when closing the connections in the group.

  • reason (str) – The reason closing the client connection.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

close_user_connections(hub: str, user_id: str, **kwargs: Any)None

Close connections for the specific user.

Close connections for the specific user.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • user_id (str) – The user Id.

Keyword Arguments
  • excluded (list[str]) – Exclude these connectionIds when closing the connections for the user.

  • reason (str) – The reason closing the client connection.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

connection_exists(hub: str, connection_id: str, **kwargs: Any)None

Check if the connection with the given connectionId exists.

Check if the connection with the given connectionId exists.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • connection_id (str) – The connection Id.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

classmethod from_connection_string(connection_string: Type[ClientType], **kwargs: str)ClientType[source]

Create a new WebPubSubServiceClient from a connection string.

Parameters

connection_string (str) – Connection string

Return type

WebPubSubServiceClient

generate_client_token(hub: str, **kwargs: Any)Any

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

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

Parameters

hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

Keyword Arguments
  • user_id (str) – User Id.

  • role (list[str]) – Roles that the connection with the generated token will have.

  • minutes_to_expire (int) – The expire time of the generated token.

Returns

JSON object

Return type

Any

Raises

~azure.core.exceptions.HttpResponseError

Example

# response body for status code(s): 200
response.json() == {
    "token": "str"  # Optional. The token value for the WebSocket client to connect to the service.
}
grant_permission(hub: str, permission: str, connection_id: str, **kwargs: Any)None

Grant permission to the connection.

Grant permission to the connection.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • permission (str) – The permission: current supported actions are joinLeaveGroup and sendToGroup. Possible values are: “sendToGroup” or “joinLeaveGroup”.

  • connection_id (str) – Target connection Id.

Keyword Arguments

target_name (str) – The meaning of the target depends on the specific permission. For joinLeaveGroup and sendToGroup, targetName is a required parameter standing for the group name.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

group_exists(hub: str, group: str, **kwargs: Any)None

Check if there are any client connections inside the given group.

Check if there are any client connections inside the given group.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • group (str) – Target group name, which length should be greater than 0 and less than 1025.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

remove_connection_from_group(hub: str, group: str, connection_id: str, **kwargs: Any)None

Remove a connection from the target group.

Remove a connection from the target group.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • group (str) – Target group name, which length should be greater than 0 and less than 1025.

  • connection_id (str) – Target connection Id.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

remove_user_from_all_groups(hub: str, user_id: str, **kwargs: Any)None

Remove a user from all groups.

Remove a user from all groups.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • user_id (str) – Target user Id.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

remove_user_from_group(hub: str, group: str, user_id: str, **kwargs: Any)None

Remove a user from the target group.

Remove a user from the target group.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • group (str) – Target group name, which length should be greater than 0 and less than 1025.

  • user_id (str) – Target user Id.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

revoke_permission(hub: str, permission: str, connection_id: str, **kwargs: Any)None

Revoke permission for the connection.

Revoke permission for the connection.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • permission (str) – The permission: current supported actions are joinLeaveGroup and sendToGroup. Possible values are: “sendToGroup” or “joinLeaveGroup”.

  • connection_id (str) – Target connection Id.

Keyword Arguments

target_name (str) – The meaning of the target depends on the specific permission. For joinLeaveGroup and sendToGroup, targetName is a required parameter standing for the group name.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

send_request(request: HttpRequest, **kwargs: Any)HttpResponse[source]

Runs the network request through the client’s chained policies.

>>> from azure.core.rest import HttpRequest
>>> request = HttpRequest("GET", "https://www.example.org/")
<HttpRequest [GET], url: 'https://www.example.org/'>
>>> response = client.send_request(request)
<HttpResponse: 200 OK>

For more information on this code flow, see https://aka.ms/azsdk/python/protocol/quickstart

Parameters

request (HttpRequest) – The network request you want to make. Required.

Keyword Arguments

stream (bool) – Whether the response payload will be streamed. Defaults to False.

Returns

The response of your network call. Does not do error handling on your response.

Return type

HttpResponse

send_to_all(hub: str, message: Union[IO, str], **kwargs: Any)None

Broadcast content inside request body to all the connected client connections.

Broadcast content inside request body to all the connected client connections.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • message (IO or str) – The payload body.

Keyword Arguments
  • excluded (list[str]) – Excluded connection Ids.

  • content_type (str) – Media type of the body sent to the API. Default value is “application/json”. Allowed values are: “application/json”, “application/octet-stream”, “text/plain.”

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

send_to_connection(hub: str, connection_id: str, message: Union[IO, str], **kwargs: Any)None

Send content inside request body to the specific connection.

Send content inside request body to the specific connection.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • connection_id (str) – The connection Id.

  • message (IO or str) – The payload body.

Keyword Arguments

content_type (str) – Media type of the body sent to the API. Default value is “application/json”. Allowed values are: “application/json”, “application/octet-stream”, “text/plain.”

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

send_to_group(hub: str, group: str, message: Union[IO, str], **kwargs: Any)None

Send content inside request body to a group of connections.

Send content inside request body to a group of connections.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • group (str) – Target group name, which length should be greater than 0 and less than 1025.

  • message (IO or str) – The payload body.

Keyword Arguments
  • excluded (list[str]) – Excluded connection Ids.

  • content_type (str) – Media type of the body sent to the API. Default value is “application/json”. Allowed values are: “application/json”, “application/octet-stream”, “text/plain.”

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

send_to_user(hub: str, user_id: str, message: Union[IO, str], **kwargs: Any)None

Send content inside request body to the specific user.

Send content inside request body to the specific user.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • user_id (str) – The user Id.

  • message (IO or str) – The payload body.

Keyword Arguments

content_type (str) – Media type of the body sent to the API. Default value is “application/json”. Allowed values are: “application/json”, “application/octet-stream”, “text/plain.”

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

user_exists(hub: str, user_id: str, **kwargs: Any)None

Check if there are any client connections connected for the given user.

Check if there are any client connections connected for the given user.

Parameters
  • hub (str) – Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

  • user_id (str) – Target user Id.

Returns

None

Return type

None

Raises

~azure.core.exceptions.HttpResponseError

azure.messaging.webpubsubservice.build_authentication_token(endpoint: str, hub: str, **kwargs: Any)Dict[Any][source]

Build an authentication token for the given endpoint, hub using the provided key.

Keyword Arguments
  • endpoint – connetion string or HTTP or HTTPS endpoint for the WebPubSub service instance.

  • hub – The hub to give access to.

  • accesskey – Key to sign the token with. Required if endpoint is not a connection string

  • ttl – Optional ttl timedelta for the token. Default is 1 hour.

  • user – Optional user name (subject) for the token. Default is no user.

  • roles – Roles for the token.

Returns

~dict containing the web socket endpoint, the token and a url with the generated access token.

Return type

~dict

Example: >>> build_authentication_token(endpoint=’https://contoso.com/api/webpubsub’, hub=’theHub’, key=’123’) {

‘baseUrl’: ‘wss://contoso.com/api/webpubsub/client/hubs/theHub’, ‘token’: ‘eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ…’, ‘url’: ‘wss://contoso.com/api/webpubsub/client/hubs/theHub?access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ…’

}