Skip to content

Commit b12dcac

Browse files
Merge pull request #297492 from terencefan/tefa/webpubsub-client-python-code-sample
update webpubsub client python code samples
2 parents 7f90c3c + a4a96fd commit b12dcac

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

articles/azure-web-pubsub/reference-client-sdk-python.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ As shown in the diagram, the client has the permissions to send messages to and
4848
```python
4949
from azure.messaging.webpubsubclient import WebPubSubClient
5050

51-
client = WebPubSubClient("<<client-access-url>>")
51+
client = WebPubSubClient("<client-access-url>")
5252
with client:
5353
# The client can join/leave groups, send/receive messages to and from those groups all in real-time
5454
...
@@ -59,11 +59,13 @@ with client:
5959
A client can only receive messages from groups that it has joined and you need to add a callback to specify the logic when receiving messages.
6060

6161
```python
62+
from azure.messaging.webpubsubclient.models import CallbackType
63+
6264
# ...continues the code snippet from above
6365

6466
# Registers a listener for the event 'group-message' early before joining a group to not miss messages
6567
group_name = "group1";
66-
client.on("group-message", lambda e: print(f"Received message: {e.data}"));
68+
client.subscribe(CallbackType.GROUP_MESSAGE, lambda e: print(f"Received message: {e.data}"));
6769

6870
# A client needs to join the group it wishes to receive messages from
6971
client.join_group(groupName);
@@ -85,31 +87,39 @@ client.send_to_group(group_name, "hello world", "text");
8587
1. When a client is successfully connected to your Web PubSub resource, the `connected` event is triggered.
8688

8789
```python
88-
client.on("connected", lambda e: print(f"Connection {e.connection_id} is connected"))
90+
from azure.messaging.webpubsubclient.models import CallbackType
91+
92+
client.subscribe(CallbackType.CONNECTED, lambda e: print(f"Connection {e.connection_id} is connected"))
8993
```
9094

9195
2. When a client is disconnected and fails to recover the connection, the `disconnected` event is triggered.
9296

9397
```python
94-
client.on("disconnected", lambda e: print(f"Connection disconnected: {e.message}"))
98+
from azure.messaging.webpubsubclient.models import CallbackType
99+
100+
client.subscribe(CallbackType.DISCONNECTED, lambda e: print(f"Connection disconnected: {e.message}"))
95101
```
96102

97103
3. The `stopped` event is triggered when the client is disconnected **and** the client stops trying to reconnect. This usually happens after the `client.stop()` is called, or `auto_reconnect` is disabled or a specified limit to trying to reconnect has reached. If you want to restart the client, you can call `client.start()` in the stopped event.
98104

99105
```python
100-
client.on("stopped", lambda : print("Client has stopped"))
106+
from azure.messaging.webpubsubclient.models import CallbackType
107+
108+
client.subscribe(CallbackType.STOPPED, lambda : print("Client has stopped"))
101109
```
102110

103111
### A client consumes messages from the application server or joined groups
104112

105113
A client can add callbacks to consume messages from your application server or groups. Note, for `group-message` event the client can _only_ receive group messages that it has joined.
106114

107115
```python
116+
from azure.messaging.webpubsubclient.models import CallbackType
117+
108118
# Registers a listener for the "server-message". The callback is invoked when your application server sends message to the connectionID, to or broadcast to all connections.
109-
client.on("server-message", lambda e: print(f"Received message {e.data}"))
119+
client.subscribe(CallbackType.CONNECTED, lambda e: print(f"Received message {e.data}"))
110120

111121
# Registers a listener for the "group-message". The callback is invoked when the client receives a message from the groups it has joined.
112-
client.on("group-message", lambda e: print(f"Received message from {e.group}: {e.data}"))
122+
client.subscribe(CallbackType.GROUP_MESSAGE, lambda e: print(f"Received message from {e.group}: {e.data}"))
113123
```
114124
---
115125
### Handle rejoin failure
@@ -120,11 +130,13 @@ However, you should be aware of `auto_rejoin_groups`'s limitations.
120130
- "rejoin group" operations may fail due to various reasons, for example, the client doesn't have permission to join the groups. In such cases, you need to add a callback to handle this failure.
121131

122132
```python
133+
from azure.messaging.webpubsubclient.models import CallbackType
134+
123135
# By default auto_rejoin_groups=True. You can disable it by setting to False.
124136
client = WebPubSubClient("<client-access-url>", auto_rejoin_groups=True);
125137

126138
# Registers a listener to handle "rejoin-group-failed" event
127-
client.on("rejoin-group-failed", lambda e: print(f"Rejoin group {e.group} failed: {e.error}"))
139+
client.subscribe(CallbackType.REJOIN_GROUP_FAILED, lambda e: print(f"Rejoin group {e.group} failed: {e.error}"))
128140
```
129141

130142
### Operation and retry
@@ -133,9 +145,9 @@ By default, the operation such as `client.join_group()`, `client.leave_group()`,
133145

134146
```python
135147
try:
136-
client.join_group(group_name)
148+
client.join_group(group_name)
137149
except SendMessageError as e:
138-
client.join_group(group_name, ack_id=e.ack_id)
150+
client.join_group(group_name, ack_id=e.ack_id)
139151
```
140152

141153
## Troubleshooting

0 commit comments

Comments
 (0)