Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions docs/tutorial/10-server-delivery.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,55 @@ The client will then keep track of the offset:

And finally the server will send the missing messages upon (re)connection:

<Tabs groupId="lang">
<TabItem value="cjs" label="CommonJS" default>

```js title="index.js"
// [...]

// highlight-next-line
io.on('connection', async (socket) => {
// highlight-next-line
socket.on('chat message', async (msg) => {
let result;
try {
result = await db.run('INSERT INTO messages (content) VALUES (?)', msg);
} catch (e) {
// TODO handle the failure
return;
}
io.emit('chat message', msg, result.lastID);
});

// highlight-start
if (!socket.recovered) {
// if the connection state recovery was not successful
try {
await db.each('SELECT id, content FROM messages WHERE id > ?',
[socket.handshake.auth.serverOffset || 0],
(_err, row) => {
socket.emit('chat message', row.content, row.id);
}
)
} catch (e) {
// something went wrong
}
}
// highlight-end
});

// [...]
```

</TabItem>
<TabItem value="mjs" label="ES modules">

```js title="index.js"
// [...]

// highlight-next-line
io.on('connection', async (socket) => {
// highlight-next-line
socket.on('chat message', async (msg) => {
let result;
try {
Expand Down Expand Up @@ -307,6 +352,9 @@ io.on('connection', async (socket) => {
// [...]
```

</TabItem>
</Tabs>

Let's see it in action:

<video controls width="100%"><source src="/videos/tutorial/server-delivery.mp4" /></video>
Expand Down