Skip to content

Commit 1355194

Browse files
committed
Update reliable protocol
1 parent a5b89fd commit 1355194

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,27 @@ ms.date: 12/15/2021
1212

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

15+
> [!NOTE]
16+
> Reliable protocols are still in preview. Some changes are expected in future.
17+
1518
## Reliable Protocol
1619

1720
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.
1821

22+
## Initialization
23+
24+
To use reliable subprotocols, you must set subprotocol when constructing Websocket connections. In JavaScript, you can use as following:
25+
26+
- Use Json reliable subprotocol
27+
```js
28+
var pubsub = new WebSocket('wss://test.webpubsub.azure.com/client/hubs/hub1', 'json.reliable.webpubsub.azure.v1');
29+
```
30+
31+
- Use Protobuf reliable subprotocol
32+
```js
33+
var pubsub = new WebSocket('wss://test.webpubsub.azure.com/client/hubs/hub1', 'protobuf.reliable.webpubsub.azure.v1');
34+
```
35+
1936
## Reconnection
2037

2138
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`.
@@ -65,7 +82,7 @@ A sample ack response:
6582

6683
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.
6784

68-
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`.
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.
6986

7087
```json
7188
{
@@ -81,7 +98,7 @@ However, In some cases, Service meets some transient internal error and the mess
8198

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

84-
Service's ack may be dropped because of WebSockets connection dropped. So, publishers should wait for ack with timeout, and resend message with the same message-id after timeout. If the message has actually processed by the service, it will response ack with `Duplicate` and publishers should stop resending this message again.
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.
85102

86103
```json
87104
{
@@ -99,16 +116,16 @@ Service's ack may be dropped because of WebSockets connection dropped. So, publi
99116

100117
## Subscriber
101118

102-
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. Messages send to subscribers contains `sequenceId` and subscribers must ack the sequence-id with sequence ack message:
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:
103120

104121
A sample sequence ack:
105122
```json
106123
{
107-
"type": "ack",
124+
"type": "sequenceAck",
108125
"sequenceId": 1
109126
}
110127
```
111128

112-
The sequence-id is a uint64 incremental number with-in a connection-id session. And don't expect the number must be consecutive. 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 quick 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.
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 selective 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.
113130

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

articles/azure-web-pubsub/reference-json-reliable-webpubsub-subprotocol.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ This document describes the subprotocol `json.reliable.webpubsub.azure.v1`.
1414

1515
When the client is using this subprotocol, both outgoing data frame and incoming data frame are expected to be **JSON** payloads.
1616

17+
> [!NOTE]
18+
> Reliable protocols are still in preview. Some changes are expected in future.
19+
1720
## Overview
1821

1922
Subprotocol `json.reliable.webpubsub.azure.v1` empowers the client to have a high reliable message delivery experience under network issues and do a publish-subscribe (PubSub) directly instead of doing a round trip to the upstream server. The WebSocket connection with the `json.reliable.webpubsub.azure.v1` subprotocol is called a Reliable PubSub WebSocket client.
@@ -37,7 +40,7 @@ Format:
3740

3841
```json
3942
{
40-
"type": "ack",
43+
"type": "sequenceAck",
4144
"sequenceId": "<sequenceId>",
4245
}
4346
```
@@ -48,7 +51,7 @@ Reliable PubSub WebSocket client must send sequence ack message once it received
4851

4952
## Responses
5053

51-
Messages received by the client can be several types: `ack`, `message`, and `system`. All response messages have `sequenceId` property. Client must send [Sequence Ack](#sequence-ack) to the service once it receives a message.
54+
Messages received by the client can be several types: `ack`, `message`, and `system`. Messages with type `message` have `sequenceId` property. Client must send [Sequence Ack](#sequence-ack) to the service once it receives a message.
5255

5356
### Ack response
5457

@@ -57,7 +60,6 @@ If the request contains `ackId`, the service will return an ack response for thi
5760
Format:
5861
```json
5962
{
60-
"sequenceId": 1,
6163
"type": "ack",
6264
"ackId": 1, // The ack id for the request to ack
6365
"success": false, // true or false
@@ -153,7 +155,6 @@ When the connection connects to service.
153155

154156
```json
155157
{
156-
"sequenceId": 1,
157158
"type": "system",
158159
"event": "connected",
159160
"userId": "user1",
@@ -176,7 +177,6 @@ When the server closes the connection, or when the service declines the client.
176177

177178
```json
178179
{
179-
"sequenceId": 1,
180180
"type": "system",
181181
"event": "disconnected",
182182
"message": "reason"

articles/azure-web-pubsub/reference-protobuf-reliable-webpubsub-subprotocol.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ This document describes the subprotocol `protobuf.reliable.webpubsub.azure.v1`.
1414

1515
When a client is using this subprotocol, both the outgoing and incoming data frames are expected to be protocol buffers (protobuf) payloads.
1616

17+
> [!NOTE]
18+
> Reliable protocols are still in preview. Some changes are expected in future.
19+
1720
## Overview
1821

1922
Subprotocol `protobuf.reliable.webpubsub.azure.v1` empowers the client to have a high reliable message delivery experience under network issues and do a publish-subscribe (PubSub) directly instead of doing a round trip to the upstream server. The WebSocket connection with the `protobuf.reliable.webpubsub.azure.v1` subprotocol is called a Reliable PubSub WebSocket client.
@@ -53,6 +56,7 @@ message UpstreamMessage {
5356
string group = 1;
5457
optional uint64 ack_id = 2;
5558
MessageData data = 3;
59+
optional bool no_echo = 4;
5660
}
5761
5862
message EventMessage {
@@ -109,7 +113,6 @@ message DownstreamMessage {
109113
uint64 ack_id = 1;
110114
bool success = 2;
111115
optional ErrorMessage error = 3;
112-
uint64 sequence_id = 4;
113116
114117
message ErrorMessage {
115118
string name = 1;
@@ -134,18 +137,16 @@ message DownstreamMessage {
134137
string connection_id = 1;
135138
string user_id = 2;
136139
string reconnection_token = 3;
137-
uint64 sequence_id = 4;
138140
}
139141
140142
message DisconnectedMessage {
141143
string reason = 2;
142-
uint64 sequence_id = 3;
143144
}
144145
}
145146
}
146147
```
147148

148-
Messages received by the client can be in any of three types: `ack`, `message`, or `system`. All response messages have `sequence_id` property. Client must send [Sequence Ack](#sequence-ack) to the service once it receives a message.
149+
`AckMessage` has `sequence_id` property. Client must send [Sequence Ack](#sequence-ack) to the service once it receives a message.
149150

150151
### Ack response
151152

0 commit comments

Comments
 (0)