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

Use proper status codes #105

Closed
hanspagel opened this issue May 18, 2021 · 2 comments
Closed

Use proper status codes #105

hanspagel opened this issue May 18, 2021 · 2 comments

Comments

@hanspagel
Copy link
Contributor

I think we’re not using status codes for the close event right now. Leaving this here as a note to check what we’re doing now, and if we can improve that.

https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent

@hanspagel
Copy link
Contributor Author

I think we’d need to improve the client to handle those errors.

yjs/y-websocket#67

@hanspagel
Copy link
Contributor Author

hanspagel commented Jun 9, 2021

When the hocuspocus onConnect hooks rejects the connection, the connection will now be closed with 4403 Forbidden. I’m using the native WebSocket CloseEvent to pass a status code and a reason.

Unfortunately, the original y-websocket doesn't pass the WebSocket CloseEvent. I’ve fiddled around with a fork of y-websocket to play around with a few ideas, like this one here. The first rough version is available on npm.

import { HocuspocusProvider } from '@hocuspocus/provider'

const provider = new HocuspocusProvider({
  url: 'ws://127.0.0.1:1234',
  name: 'hocuspocus-demo',
  document: this.ydoc,
  onClose: ({ event }) => {
    console.log('[close]', event.code, event.reason)
  },
})

// Output: [close] 4403 Forbidden

That lets you act accordingly, for example when a user isn’t authorized to connect.

I have a few more ideas for the provider and implemented already a few things, but I’m still not sure how far I’ll take that. For now it’s still compatible with all other Y.js providers, just with a few more features and I’d like to keep it that way.

It’s likely that I sent a PR for the status codes to y-websocket once it’s proven to be helpful.

One thing though: Play around with it and send your feedback, but don’t use it in production. It’s the first release, and probably has a few bugs.

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

No branches or pull requests

1 participant