Skip to content

Commit b481e65

Browse files
committed
+ protocol: presence
1 parent 54c4896 commit b481e65

1 file changed

Lines changed: 92 additions & 5 deletions

File tree

docs/misc/action_cable_protocol.md

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Usually, in this case we can only specify the `since` parameter.
168168

169169
In response, the server MUST first confirm the subscription and then execute the history request. If the subscription is rejected, no history request is made.
170170

171-
### New message types
171+
### New message types: `confirm_history` and `reject_history`
172172

173173
Two new message types (server-client) are added:
174174

@@ -177,7 +177,7 @@ Two new message types (server-client) are added:
177177

178178
Both messages act as acknowledgments for the `history` command and contain the `identifier` key. The `confirm_history` message is sent to the client to indicate that the requested historical messages for the channel have been successfully sent to the client. The `reject_history` indicates that the server failed to retrieve the requested messages and no historical message have been sent (the client must implement a fallback mechanism to restore the consistency).
179179

180-
### Incoming messages extensions
180+
### History metadata in broadcasted messages
181181

182182
Broadcasted messages MAY contain metadata regarding their position in the stream. This information MUST be used with the subsequent `history` requests:
183183

@@ -195,9 +195,9 @@ Broadcasted messages MAY contain metadata regarding their position in the stream
195195
}
196196
```
197197

198-
**NOTE:** The messages are not guaranteed to be in order (due to concurrent broadcasts), so, offsets may be non-monotonic. It's the client responsibility to keep track of offsets. Also, there is a small chance that the same message may arrive twice (from broadcast and from the history); to provide exactly-once delivery guarantees, the client MUST keep track of seen offsets and ignore duplicates.
198+
**NOTE:** Offsets are assumed to be in order (however, some history backends may lift this requirement, check the corresponding documentation). There is a small chance that the same message may arrive twice (from broadcast and from the history); to provide exactly-once delivery guarantees, the client MUST keep track of seen offsets and ignore duplicates.
199199

200-
### Handshake extensions
200+
### Handshake metadata
201201

202202
During the handshake, the server MAY send a unique _session id_ along with the welcome message:
203203

@@ -231,7 +231,7 @@ The optional `restored_ids` field contains the list of channel identifiers that
231231

232232
The `pong` command MAY be sent in response to the `ping` message if the server requires pongs. It could be used to improve broken connections detection.
233233

234-
### New command: `whisper` <img class='pro-badge' src='/assets/new.svg' alt='new' />
234+
### New command: `whisper`
235235

236236
The `whisper` can be used to publish broadcast messages from the client (if the whisper stream has been configured for it) to a particular _channel_.
237237

@@ -253,3 +253,90 @@ For example:
253253
```
254254

255255
**IMPORTANT**: Unlike actions (`message` command), the data is not JSON-serialized. It's broadcasted to connected clients as is.
256+
257+
### New commands: `join` and `leave`
258+
259+
Presence commands allow the client to join or leave the presence set for a given channel:
260+
261+
```js
262+
{
263+
"command": "join",
264+
"identifier": "<subscription identifier>",
265+
"presence": {
266+
"id": "<user id within the presence set>",
267+
"info": "..." // any object or string
268+
}
269+
}
270+
271+
{
272+
"command": "leave",
273+
"identifier": "<subscription identifier>",
274+
"presence": {
275+
"id": "<user id within the presence set>"
276+
}
277+
}
278+
```
279+
280+
### New commands: `presence`
281+
282+
The presence command is used to request the current presence state for a given channel:
283+
284+
```js
285+
{
286+
"command": "presence",
287+
"identifier": "<subscription identifier>",
288+
}
289+
```
290+
291+
You can also provide the "data" field with some configuration options:
292+
293+
```js
294+
{
295+
"command": "presence",
296+
"identifier": "<subscription identifier>",
297+
"data": {
298+
"return_records": true // set to false to return only totals, see below
299+
}
300+
}
301+
```
302+
303+
### New message type: `presence`
304+
305+
Presence messages have the following formats:
306+
307+
```js
308+
{
309+
"type": "presence",
310+
"identifier": "<subscription identifier>",
311+
"message": {
312+
"id": "<user id within the presence set>",
313+
"info": "..." // any object or string
314+
"type": "join"
315+
}
316+
}
317+
318+
{
319+
"type": "presence",
320+
"identifier": "<subscription identifier>",
321+
"message": {
322+
"id": "<user id within the presence set>",
323+
"type": "leave"
324+
}
325+
}
326+
327+
{
328+
"type": "presence",
329+
"identifier": "<subscription identifier>",
330+
"message": {
331+
"total": 123, // total number of present users
332+
"records": [
333+
{
334+
"id": "...",
335+
"info": "..."
336+
},
337+
...
338+
], # presence records (missing if return_records is false)
339+
"type": "info"
340+
}
341+
}
342+
```

0 commit comments

Comments
 (0)