Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to listen to client disconnects? #424

Closed
danijar opened this issue Jan 7, 2021 · 7 comments
Closed

How to listen to client disconnects? #424

danijar opened this issue Jan 7, 2021 · 7 comments

Comments

@danijar
Copy link

danijar commented Jan 7, 2021

There is a middleware for listening to new connections but I didn't find any to listen to the event of closed or lost websocket connections.

We would like to use this in a custom presence implementation that stores a dictionary of information about the currently connected users in the document. Users should be removed from the dictionary when they disconnect.

@alecgibson
Copy link
Collaborator

Why not use ShareDB's built-in presence?

@danijar
Copy link
Author

danijar commented Jan 7, 2021

It doesn't support JSON docs and the idea of transformPresence seemed overly complicated to us. We show cursors of other users as CodeMirror bookmarks so they already flow with the text without need for manual transformation. Wouldn't listening to disconnects be a more generally useful feature as well?

@alecgibson
Copy link
Collaborator

alecgibson commented Jan 7, 2021

const connections = {};

const presence = shareDb.getPresence('my-channel');
presence.subscribe();

presence.on('receive', (presenceId, payload) => {
  // payload will be the arbitrary metadata you sent (see below)
  if (payload) connections[presenceId] = payload;
  // payload will be null if the remote presence has been disconnected or destroyed
  else delete connections[presenceId]
});

const localPresence = presence.create('some-presence-id');
localPresence.submit({
  // Whatever arbitrary metadata you want to send
});

@danijar
Copy link
Author

danijar commented Jan 8, 2021

Thanks! We switched to the ShareDB presence functionality by using the doc UUID as channel name. The cursors are moving much faster now. The only remaining problem is that the presence information is shared faster than the document edits, so the client inserts the new cursor position in the old version of the text. The next text arrives after that and is inserted to the right of the cursor, so CodeMirror does not move the cursor to the end of the text. Is this the type of problem that the rich-text presence transformation solves? If so, is there a simple way for us to use that feature?

@alecgibson
Copy link
Collaborator

Is this the type of problem that the rich-text presence transformation solves?

Yes, this is exactly the problem that DocPresence solves.

is there a simple way for us to use that feature?

The "simplest" way is obviously to use the rich-text type, although I appreciate that that's not a very practical answer. Sadly we don't yet have a text0 implementation yet, so you'd have to roll your own — Pull Requests are always welcome!

You can probably reuse most of the code from rich-text, which is split across the quill-delta and rich-text libraries.

If you can get it working for text0, then we can probably adapt the code @curran pasted here to apply to json0 sub-types.

@danijar
Copy link
Author

danijar commented Jan 8, 2021

Thanks again! We don't have the capacity and knowledge to implement that feature at the moment unfortunately.

For now, we approximately fixed the off by one cursors by re-sending the presence again with 1 second delay after every cursor activity. While a collaborator is typing, their cursor is sometimes off by one but after one second it will at least land in the correct spot.

It would be great to have doc presence support for string fields of json0 types in ShareDB in the future and then switch to that.

@danijar danijar closed this as completed Jan 8, 2021
@curran
Copy link
Contributor

curran commented Jan 8, 2021

It's good to know there's demand for presence in JSON0. I hope to take a crack at implementing it!

Tracked also in ottypes/json0#30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants