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

question: Running on the same port as an HTTP server #173

Open
alexgleason opened this issue Apr 3, 2023 · 2 comments
Open

question: Running on the same port as an HTTP server #173

alexgleason opened this issue Apr 3, 2023 · 2 comments

Comments

@alexgleason
Copy link

Summary

Hi, I'm developing a project people will self-host, and for simplicity it would be better if I could run the websocket server on the same port as my main application. Is there a way to just get a request handler from this library?

@crookse
Copy link
Member

crookse commented Apr 7, 2023

Hey @alexgleason, currently there is no way to get a request handler from this library. I'd like to help further, but without much context, I won't be able to provide a working solution that could fit your needs. If your main application is using an HTTP server you control (e.g., an API backend you're writing), you should be able to add an endpoint to the API that takes requests and upgrades them to websocket connections. This would be done on the same port.

@alexgleason
Copy link
Author

Thank you @crookse. I'm wanting to use this library with a Deno HTTP server such as Hono or Oak. Something like this maybe?

const app = new Hono();
const ws = new Server();

app.get('/api/v1/streaming', ws.handler);

ws.on('hello', (e) => {
  // do stuff
});

This example exposes a handler function on the Server object, with a type like (req: Request) => void. You would use instead of calling server.run().

For prior art, socket.io seems to offer an API like this (but socket.io is not compatible with normal websockets, making Wocket the better option): https://github.com/socketio/socket.io#in-conjunction-with-express

The reason I want to use this library instead of something custom is so I could leverage the powerful channels and broadcast APIs.

Also, my code is here: https://gitlab.com/soapbox-pub/ditto/-/blob/develop/src/app.ts

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

No branches or pull requests

2 participants