You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-web-pubsub/howto-develop-reliable-clients.md
+33-21Lines changed: 33 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,37 +5,43 @@ author: chenyl
5
5
ms.author: chenyl
6
6
ms.service: azure-web-pubsub
7
7
ms.topic: reference
8
-
ms.date: 12/15/2021
8
+
ms.date: 01/12/2023
9
9
---
10
10
11
11
# Create reliable Websocket with subprotocol
12
12
13
-
Websocket client connections may drop due to intermittent network issue and when connections drop, messages will also be lost. In a pubsub system, publishers are decoupled from subscribers, so publishers hard to detect subscribers' drop or message loss. It's crucial for clients to overcome intermittent network issue and keep the reliability of message delivery. To achieve that, you can create a reliable Websocket client with the help of reliable subprotocols.
13
+
When Websocket client connections drop due to intermittent network issues, messages can be lost. In a pub/sub system, publishers are decoupled from subscribers, so publishers may not detect a subscribers' dropped connection or message loss. It's crucial for clients to overcome intermittent network issues and maintain reliable message delivery. To achieve that, you can create a reliable Websocket client with the help of reliable Azure Web PubSub subprotocols.
14
14
15
15
> [!NOTE]
16
-
> Reliable protocols are still in preview. Some changes are expected in future.
16
+
> Reliable protocols are still in preview. Some changes are expected in the future.
17
17
18
18
## Reliable Protocol
19
19
20
-
Service supports two reliable subprotocols `json.reliable.webpubsub.azure.v1` and `protobuf.reliable.webpubsub.azure.v1`. Clients must follow the protocol, mainly including the part of reconnection, publisher and subscriber to achieve the reliability, or the message delivery may not work as expected or the service may terminate the client as it violates the protocol spec.
20
+
The Web PubSub service supports two reliable subprotocols `json.reliable.webpubsub.azure.v1` and `protobuf.reliable.webpubsub.azure.v1`. Clients must follow the publisher, subscriber, and reconnection parts of the subprotocol to achieve reliability. Failing to properly implement the subprotocol may result in the message delivery not working as expected or the service terminating the client due to protocol violations.
21
21
22
22
## Initialization
23
23
24
-
To use reliable subprotocols, you must set subprotocol when constructing Websocket connections. In JavaScript, you can use as following:
24
+
To use reliable subprotocols, you must set the subprotocol when constructing Websocket connections. In JavaScript, you can use the following code:
25
+
26
+
- Use Json reliable subprotocol:
25
27
26
-
- Use Json reliable subprotocol
27
28
```js
28
29
var pubsub =newWebSocket('wss://test.webpubsub.azure.com/client/hubs/hub1', 'json.reliable.webpubsub.azure.v1');
29
30
```
30
31
31
-
- Use Protobuf reliable subprotocol
32
+
- Use Protobuf reliable subprotocol:
33
+
32
34
```js
33
35
var pubsub = new WebSocket('wss://test.webpubsub.azure.com/client/hubs/hub1', 'protobuf.reliable.webpubsub.azure.v1');
34
36
```
35
37
36
38
## Reconnection
37
39
38
-
Websocket connections relay on TCP, so if the connection doesn't drop, all messages should be lossless and in order. When facing network issue and connections drop, all the status such as group and message info are kept by the service and wait for reconnection to recover. A Websocket connection owns a session in the service and the identifier is `connectionId`. Reconnection is the basis of achieving reliability and must be implemented. When a new connection connects to the service using reliable subprotocols, the connection will receive a `Connected` message contains `connectionId` and `reconnectionToken`.
40
+
Reconnection is the basis of achieving reliability and must be implemented when using the `json.reliable.webpubsub.azure.v1` and `protobuf.reliable.webpubsub.azure.v1` protocols.
41
+
42
+
Websocket connections rely on TCP. When the connection doesn't drop, messages are lossless and delivered in order. To prevent message loss over dropped connections, the Web PubSub service retains the connection status information, including group and message information. This information is used to restore the client on reconnection
43
+
44
+
When the client reconnects to the service using reliable subprotocols, the client will receive a `Connected` message containing the `connectionId` and `reconnectionToken`. The `connectionId` identifies the session of the connection in the service.
39
45
40
46
```json
41
47
{
@@ -46,21 +52,25 @@ Websocket connections relay on TCP, so if the connection doesn't drop, all messa
46
52
}
47
53
```
48
54
49
-
Once the WebSocket connection dropped, the client should first try to reconnect with the same `connectionId` to keep the session. Clients don't need to negotiate with server and obtain the `access_token`. Instead, reconnection should make a websocket connect request to service directly with`connection_id` and `reconnection_token`with the following uri:
55
+
Once the WebSocket connection drops, the client should try to reconnect with the same `connectionId` to restore the same session. Clients don't need to negotiate withthe server and obtain the `access_token`. Instead, on reconnection the client should make a WebSocket connect request directly to the service withthe service host name, `connection_id`, and `reconnection_token`:
Reconnection may fail as network issue hasn't been recovered yet. Client should keep retrying reconnecting until
56
-
1. Websocket connection closed with status code 1008. The status code means the connectionId has been removed from the service.
57
-
2. Reconnection failure keeps more than 1 minute.
61
+
Reconnection may fail if the network issue hasn't been recovered yet. The client should keep retrying to reconnect until:
62
+
63
+
1. The Websocket connection is closed with status code 1008. The status code means the connectionId has been removed from the service.
64
+
2. A reconnection failure continues to occur for more than 1 minute.
58
65
59
66
## Publisher
60
67
61
-
Clients who send events to event handler or publish message to other clients are called publishers in the document. Publishers should set `ackId` to the message to get acknowledged from the service about whether the message publishing success or not. The `ackId` in message is the identifier of the message, which means different messages should use different `ackId`s, while resending message should keep the same `ackId` for the service to de-duplicate.
68
+
Clients that send events to event handlers or publish messages to other clients are called publishers. Publishers should set `ackId` in the message to receive an acknowledgment from the Web PubSub service that publishing the message was successful or not.
69
+
70
+
The `ackId` is the identifier of the message, each new message should use a unique ID. The original `ackId` should be used when resending a message.
62
71
63
72
A sample group send message:
73
+
64
74
```json
65
75
{
66
76
"type": "sendToGroup",
@@ -72,6 +82,7 @@ A sample group send message:
72
82
```
73
83
74
84
A sample ack response:
85
+
75
86
```json
76
87
{
77
88
"type": "ack",
@@ -80,10 +91,10 @@ A sample ack response:
80
91
}
81
92
```
82
93
83
-
If the service returns ack with `success: true`, the message has been processed by the service and the client can expect the message will be delivered to all subscribers.
94
+
When the Web PubSub service returns an ack response with `success: true`, the message has been processed by the service, and the client can expect the message will be delivered to all subscribers.
95
+
96
+
When the service experiences a transient internal error and the message can't be sent to subscriber, the publisher will receive an ack with`success: false`. The publisher should read the error to determine whether or not to resend the message. If the message is resent, the same `ackId` should be used.
84
97
85
-
However, In some cases, Service meets some transient internal error and the message can't be sent to subscriber. In such case, publisher will receive an ack like following and should resend message with the same `ackId` if it's necessary based on business logic.
86
-
87
98
```json
88
99
{
89
100
"type": "ack",
@@ -98,7 +109,7 @@ However, In some cases, Service meets some transient internal error and the mess
Service's ack may be dropped because of WebSockets connection dropped. So, publishers should get notified when Websocket connection drops and resend message with the same `ackId` after reconnection. If the message has actually processed by the service, it will response ack with `Duplicate`and publishers should stop resending this message again.
112
+
If the service's ack response is lost because the WebSocket connection dropped, the publisher should resend the message with the same `ackId` after reconnection. When the message was previously processed by the service, it will send an ack containing a `Duplicate` error. The publisher should stop resending this message.
102
113
103
114
```json
104
115
{
@@ -116,16 +127,17 @@ Service's ack may be dropped because of WebSockets connection dropped. So, publi
116
127
117
128
## Subscriber
118
129
119
-
Clients who receive messages sent from event handlers or publishers are called subscriber in the document. When connections drop by network issues, the service doesn't know how many messages are actually sent to subscribers. So subscribers should tell the service which message has been received. Data Messages contains`sequenceId` and subscribers must ack the sequence-id with sequence ack message:
130
+
Clients that receive messages from event handlers or publishers are called subscribers. When connections drop due to network issues, the Web PubSub service doesn't know how many messages were sent to subscribers. To determine the last message received by the subscriber, the service sends a data message containing a`sequenceId`. The subscriber responds with a sequence ack message:
120
131
121
132
A sample sequence ack:
133
+
122
134
```json
123
135
{
124
136
"type": "sequenceAck",
125
137
"sequenceId": 1
126
138
}
127
139
```
128
140
129
-
The sequence-id is a uint64 incremental number with-in a connection-id session. Subscribers should record the largest sequence-id it ever received and accept all messages with larger sequence-id and drop all messages with smaller or equal sequence-id. The sequence ack supports cumulative ack, which means if you ack `sequence-id=5`, the service will treat all messages with sequence-id smaller than 5 have already been received by subscribers. Subscribers should ack with the largest sequence-id it recorded, so that the service can skip redelivering messages that subscribers have already received.
141
+
The `sequenceId` is a uint64 incremental number in a connection-id session. Subscribers should record the largest `sequenceId` it has received, accept only messages witha larger `sequenceId`, and drop messages witha smaller or equal `sequenceId`. Thesubscriber should ackwith the largest `sequenceId` it recorded, so that the service can skip redelivering messages that subscribers have already received. For example, if the subscriber responds witha `sequenceAck`with`sequenceId: 5`, the service will only resend messages with a `sequenceId` larger than 5.
130
142
131
-
All messages are delivered to subscribers in order until the WebSockets connection drops. With sequence-id, the service can have the knowledge about how many messages subscribers have actually received across WebSockets connections with-in a connection-id session. After a WebSockets connection drop, the service will redeliver messages it should deliver but not ack-ed by the subscriber. The service hold messages that are not ack-ed with limit, if messages exceed the limit, the service will close the WebSockets connection and remove the connection-id session. Thus, subscribers should ack the sequence-id as soon as possible.
143
+
All messages are delivered to subscribers in order until the WebSocket connection drops. With`sequenceId`, the service can know how many messages subscribers have received across WebSocket connections in a session. After a WebSocket connection drops, the service will redeliver messages not acknowledged by the subscriber. The service stores a limited number of unacknowledged messages. When the number ofmessages exceed the limit, the service will close the WebSocket connection and remove the session. Thus, subscribers should ack the `sequenceId` as soon as possible.
0 commit comments