Python 用 Azure Web PubSub サービス クライアント ライブラリ

Azure Web PubSub サービスは、開発者がリアルタイムの機能とパブリッシュ-サブスクライブ パターンを使って Web アプリケーションを簡単に作成できるようにするための Azure マネージド サービスです。 サーバーとクライアント間、またはクライアント間で、リアルタイムのパブリッシュ-サブスクライブ メッセージングを必要とするあらゆるシナリオに、Azure Web PubSub サービスを使用できます。 従来のリアルタイム機能は、多くの場合、サーバーからのポーリングや HTTP 要求の送信を必要としますが、そのようなリアルタイム機能にも Azure Web PubSub サービスを使用できます。

次の図に示すように、このライブラリをアプリ サーバー側で使用して、WebSocket クライアント接続を管理できます。

The overflow diagram shows the overflow of using the service client library.

このライブラリは次のことに使うことができます。

  • ハブとグループにメッセージを送信します。
  • 特定のユーザーと接続にメッセージを送信します。
  • ユーザーと接続をグループに整理します。
  • 接続を終了します
  • 既存の接続のアクセス許可を付与、取り消し、確認します

前提条件

重要

Python 2.7 に対する Azure SDK Python パッケージのサポートは、2022 年 1 月 1 日に終了します。 詳細については、Azure SDK Python パッケージのサポートに関するページを参照してください。

パッケージをインストールする

次のコマンドを使用して、パッケージをインストールします。

python -m pip install azure-messaging-webpubsubservice

WebPubSubServiceClient を作成して認証する

接続文字列を使用して WebPubSubServiceClient を認証できます。

>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient

>>> service = WebPubSubServiceClient.from_connection_string(connection_string='<connection_string>', hub='hub')

または、サービス エンドポイントとアクセス キーを使用します。

>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> from azure.core.credentials import AzureKeyCredential

>>> service = WebPubSubServiceClient(endpoint='<endpoint>', hub='hub', credential=AzureKeyCredential("<access_key>"))

または、Microsoft Entra ID を使用します。

  1. pip を使用して azure-identity をインストールします。

  2. Webpubsub リソースで Microsoft Entra 認可を有効にします

  3. DefaultAzureCredential を使用するようにコードを更新します。

    >>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
    >>> from azure.identity import DefaultAzureCredential
    >>> service = WebPubSubServiceClient(endpoint='<endpoint>', hub='hub', credential=DefaultAzureCredential())
    

JSON 形式でメッセージをブロードキャストする

>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient

>>> service = WebPubSubServiceClient.from_connection_string('<connection_string>', hub='hub1')
>>> service.send_to_all(message = {
        'from': 'user1',
        'data': 'Hello world'
    })

WebSocket クライアントは、JSON でシリアル化されたテキスト {"from": "user1", "data": "Hello world"} を受信します:。

プレーンテキスト形式のブロードキャスト メッセージ

>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> service = WebPubSubServiceClient.from_connection_string('<connection_string>', hub='hub1')
>>> service.send_to_all(message = 'Hello world', content_type='text/plain')

WebSocket クライアントは、テキスト Hello world を受信します:。

バイナリ形式でメッセージをブロードキャストする

>>> import io
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> service = WebPubSubServiceClient.from_connection_string('<connection_string>', hub='hub')
>>> service.send_to_all(message=io.StringIO('Hello World'), content_type='application/octet-stream')

WebSocket クライアントは、バイナリ テキスト b'Hello world' を受信します:。

Logging

この SDK は Python 標準ログ ライブラリを使用します。 デバッグ情報を stdout または任意の場所に出力するようにログを構成できます。

import sys
import logging
from azure.identity import DefaultAzureCredential
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient

# Create a logger for the 'azure' SDK
logger = logging.getLogger('azure')
logger.setLevel(logging.DEBUG)

# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)

endpoint = "<endpoint>"
credential = DefaultAzureCredential()

# This WebPubSubServiceClient will log detailed information about its HTTP sessions, at DEBUG level
service = WebPubSubServiceClient(endpoint=endpoint, hub='hub', credential=credential, logging_enable=True)

同様に、logging_enable は、詳細なログ記録が WebPubSubServiceClient に対して有効になっていない場合でも、単一の呼び出しに対してそれを有効にできます。

result = service.send_to_all(..., logging_enable=True)

このログ構成を使用して、HTTP 要求と応答の詳細が stdout に出力されます。

次のステップ

その他のサンプルについては、Python サンプル用の Azure Web PubSub サービス クライアント ライブラリを参照してください。

投稿

このプロジェクトでは、投稿とご提案をお待ちしております。 ほとんどの共同作成では、共同作成者使用許諾契約書 (CLA) にご同意いただき、ご自身の共同作成内容を使用する権利を Microsoft に供与する権利をお持ちであり、かつ実際に供与することを宣言していただく必要があります。 詳細については、「Contributor License Agreement (共同作成者ライセンス契約)」を参照してください。

pull request を送信すると、CLA を提供して PR (ラベル、コメントなど) を適宜装飾する必要があるかどうかを CLA ボットが自動的に決定します。 ボットによって提供される手順に従ってください。 このアクションは、Microsoft の CLA を使用するすべてのリポジトリについて、1 回のみ行う必要があります。

このプロジェクトは、「Microsoft のオープン ソースの倫理規定」を採用しています。 詳細については、「Code of Conduct (倫理規定)」の FAQ のページを参照するか、追加の質問やコメントがある場合は オープンソースの行動規範チームにお問い合わせください。