Skip to content

Commit 6ddfb8d

Browse files
wrjgoldizruff
andauthored
Feat/participant indicator (#26)
* Enabled presence in ShareDB backend * Added error handling The WebSocketJSONStream package does not have error handling out of the box, and occasionally errors can occur if the backend needs to send updates to a client when that client is reconnecting. This caused the backend to crash. So, this error handling is required to prevent the backend from crashing. * Fixed error not filtered properly * Removed testing code --------- Co-authored-by: izruff <[email protected]>
1 parent 9cfae6f commit 6ddfb8d

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const uuid = require('uuid').v4;
99
const COLLECTION_NAME = 'sa';
1010

1111
const app = new Koa();
12-
const db = new ShareDB();
12+
const db = new ShareDB({ presence: true });
1313

1414
db.use('connect', (ctx, done) => {
1515
// use custom to store the allowed document ID and readOnly setting
@@ -76,6 +76,17 @@ app.use(async (ctx) => {
7676

7777
if (ctx.ws) {
7878
const ws = new WebSocketJSONStream(await ctx.ws());
79+
ws.on('error', (err) => {
80+
switch (err.message) {
81+
case 'WebSocket CLOSING or CLOSED.':
82+
console.log(err);
83+
break;
84+
default:
85+
console.error('Unexpected error:')
86+
console.error(err);
87+
break;
88+
}
89+
})
7990
db.listen(ws, { docId, readOnly }); // docId and readOnly is passed to 'connect' middleware as ctx.req
8091
} else {
8192
ctx.body = { docId, readOnly };

0 commit comments

Comments
 (0)