@@ -83,21 +83,39 @@ When the client is connected, it can send messages to the upstream application,
8383
8484# # Examples
8585
86- # ## Broadcast messages
86+ # ## Broadcast messages in JSON format
8787
8888```python
8989>> > from azure.messaging.webpubsubservice import WebPubSubServiceClient
90- >> > from azure.identity import DefaultAzureCredential
91- >> > from azure.core.exceptions import HttpResponseError
9290
93- >> > service = WebPubSubServiceClient(endpoint = ' <endpoint>' , credential = DefaultAzureCredential())
94- >> > with open (' file.json' , ' r' ) as f:
95- try :
96- service.send_to_all(' ahub' , content = f)
97- except HttpResponseError as e:
98- print (' service responds error: {} ' .format(e.response.json()))
91+ >> > service = WebPubSubServiceClient.from_connection_string(' <connection_string>' )
92+ >> > service.send_to_all(' hub1' , message = {
93+ ' from' : ' user1' ,
94+ ' data' : ' Hello world'
95+ })
96+ ```
97+
98+ The WebSocket client will receive JSON serialized text: `{" from" : " user1" , " data" : " Hello world" }` .
99+
100+ # ## Broadcast messages in plain-text format
99101
102+ ```python
103+ >> > from azure.messaging.webpubsubservice import WebPubSubServiceClient
104+ >> > service = WebPubSubServiceClient.from_connection_string(' <connection_string>' )
105+ >> > service.send_to_all(' hub1' , message = ' Hello world' , content_type = ' text/plain' )
106+ ```
107+
108+ The WebSocket client will receive text: `Hello world` .
109+
110+ # ## Broadcast messages in binary format
111+
112+ ```python
113+ >> > import io
114+ >> > from azure.messaging.webpubsubservice import WebPubSubServiceClient
115+ >> > service = WebPubSubServiceClient.from_connection_string(' <connection_string>' )
116+ >> > service.send_to_all(' hub1' , message = io.StringIO(' Hello World' ), content_type = ' application/octet-stream' )
100117```
118+ The WebSocket client will receive binary text: `b ' Hello world' ` .
101119
102120# # Troubleshooting
103121
@@ -138,7 +156,7 @@ Http request and response details are printed to stdout with this logging config
138156
139157# # Next steps
140158
141- Check [more samples here][awps_samples ].
159+ Check [more samples here][samples ].
142160
143161# # Contributing
144162
@@ -178,4 +196,4 @@ additional questions or comments.
178196[azure_portal]: https:// docs.microsoft.com/ azure/ azure- web- pubsub/ howto- develop- create- instance
179197[azure- key- credential]: https:// aka.ms/ azsdk- python- core- azurekeycredential
180198[aad_doc]: https:// aka.ms/ awps/ aad
181- [awps_samples ]: https:// github.com/ Azure/ azure- webpubsub / tree/ main/ samples / python
199+ [samples ]: https:// github.com/ Azure/ azure- sdk - for - python / tree/ main/ sdk / webpubsub / azure - messaging - webpubsubservice / samples
0 commit comments