diff --git a/docs/tutorial/10-server-delivery.md b/docs/tutorial/10-server-delivery.md index 23764abd..f363d20a 100644 --- a/docs/tutorial/10-server-delivery.md +++ b/docs/tutorial/10-server-delivery.md @@ -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: + + + ```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 + }); + +// [...] +``` + + + + +```js title="index.js" +// [...] + +// highlight-next-line io.on('connection', async (socket) => { + // highlight-next-line socket.on('chat message', async (msg) => { let result; try { @@ -307,6 +352,9 @@ io.on('connection', async (socket) => { // [...] ``` + + + Let's see it in action: