Skip to content

Commit 51473ea

Browse files
committed
Refresh review for howto-develop-reliable-clients
1 parent fe91bc3 commit 51473ea

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

articles/azure-web-pubsub/howto-develop-reliable-clients.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,39 @@ author: chenyl
55
ms.author: chenyl
66
ms.service: azure-web-pubsub
77
ms.topic: reference
8-
ms.date: 12/15/2021
8+
ms.date: 01/08/2023
99
---
1010

1111
# Create reliable Websocket with subprotocol
1212

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.
1414

1515
> [!NOTE]
1616
> Reliable protocols are still in preview. Some changes are expected in future.
1717
1818
## Reliable Protocol
1919

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 reconnection, publisher and subscriber parts of the protocol to achieve the reliability. Failing to follow the subprotocols, the message delivery may not work as expected or the service may terminate the client as it violates the protocol spec.
2121

2222
## Initialization
2323

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 command:
2525

2626
- Use Json reliable subprotocol
27+
2728
```js
2829
var pubsub = new WebSocket('wss://test.webpubsub.azure.com/client/hubs/hub1', 'json.reliable.webpubsub.azure.v1');
2930
```
3031

3132
- Use Protobuf reliable subprotocol
33+
3234
```js
3335
var pubsub = new WebSocket('wss://test.webpubsub.azure.com/client/hubs/hub1', 'protobuf.reliable.webpubsub.azure.v1');
3436
```
3537

3638
## Reconnection
3739

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+
Websocket connections rely on TCP, so if the connection doesn't drop, all messages should be lossless and in order. When facing network issues and connection drops, all the status, such as group and message info, are kept by the Web PubSub service for reconnection. 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 the client reconnects to the service using reliable subprotocols, the client will receive a `Connected` message containing the `connectionId` and `reconnectionToken`.
3941
4042
```json
4143
{
@@ -46,21 +48,23 @@ Websocket connections relay on TCP, so if the connection doesn't drop, all messa
4648
}
4749
```
4850
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:
51+
Once the WebSocket connection drops, 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:
5052

51-
```
53+
```text
5254
wss://<service-endpoint>/client/hubs/<hub>?awps_connection_id=<connection_id>&awps_reconnection_token=<reconnection_token>
5355
```
5456

55-
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.
57+
Reconnection may fail if the network issue hasn't been recovered yet. The client should keep retrying to reconnect until:
58+
59+
1. The Websocket connection is closed with status code 1008. The status code means the connectionId has been removed from the service.
60+
2. A reconnection failure continues to occur for more than 1 minute.
5861
5962
## Publisher
6063
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.
64+
Clients that send events to event handler 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. The `ackId` in message is the identifier of the message, each new message should use a unique ID. The original `ackId` should be used when resending a message.
6265
6366
A sample group send message:
67+
6468
```json
6569
{
6670
"type": "sendToGroup",
@@ -72,6 +76,7 @@ A sample group send message:
7276
```
7377
7478
A sample ack response:
79+
7580
```json
7681
{
7782
"type": "ack",
@@ -80,10 +85,10 @@ A sample ack response:
8085
}
8186
```
8287
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.
88+
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.
89+
90+
When the service meets some 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. If the message is resent, the same `ackId` should be used.
8491

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-
8792
```json
8893
{
8994
"type": "ack",
@@ -98,7 +103,7 @@ However, In some cases, Service meets some transient internal error and the mess
98103

99104
![Message Failure](./media/howto-develop-reliable-clients/message-failed.png)
100105

101-
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.
106+
When the service's ack response is dropped because the WebSockets connection dropped, the publisher should resend message with the same `ackId` after reconnection. When the message has previously processed by the service, it will send an ack containing a `Duplicate` error and the publisher should stop resending this message.
102107
103108
```json
104109
{
@@ -116,16 +121,17 @@ Service's ack may be dropped because of WebSockets connection dropped. So, publi
116121
117122
## Subscriber
118123
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:
124+
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:
120125

121126
A sample sequence ack:
127+
122128
```json
123129
{
124130
"type": "sequenceAck",
125131
"sequenceId": 1
126132
}
127133
```
128134

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.
135+
The `sequenceId` is a uint64 incremental number in a connection-id session. Subscribers should record the largest `sequenceId` it received, accept all messages with larger `sequenceId`, and drop all messages with smaller or equal `sequenceId`. The sequence ack supports cumulative ack, which means if you ack `sequenceId: 5`, the service will treat all messages with `sequenceId` smaller than 5 have already been received by the subscriber. The subscriber should ack with the largest `sequenceId` it recorded, so that the service can skip redelivering messages that subscribers have already received.
130136

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.
137+
All messages are delivered to subscribers in order until the WebSockets connection drops. With `sequenceId`, the service can know about how many messages subscribers have received across WebSockets connections in a connection-id session. After a WebSockets connection drops, the service will redeliver messages not acknowledged by the subscriber. The service stores a limited number of unacknowledged messages. When the number of messages exceed the limit, the service will close the WebSockets connection and remove the connection-id session. Thus, subscribers should ack the `sequenceId` as soon as possible.

0 commit comments

Comments
 (0)