Skip to content

Commit

Permalink
Feat/participant indicator (#26)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
wrjgold and izruff authored Jun 30, 2024
1 parent 9cfae6f commit 6ddfb8d
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const uuid = require('uuid').v4;
const COLLECTION_NAME = 'sa';

const app = new Koa();
const db = new ShareDB();
const db = new ShareDB({ presence: true });

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

if (ctx.ws) {
const ws = new WebSocketJSONStream(await ctx.ws());
ws.on('error', (err) => {
switch (err.message) {
case 'WebSocket CLOSING or CLOSED.':
console.log(err);
break;
default:
console.error('Unexpected error:')
console.error(err);
break;
}
})
db.listen(ws, { docId, readOnly }); // docId and readOnly is passed to 'connect' middleware as ctx.req
} else {
ctx.body = { docId, readOnly };
Expand Down

0 comments on commit 6ddfb8d

Please sign in to comment.