drashland / wocket

A WebSocket library for Deno
MIT License
104 stars 4 forks source link

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

Open alexgleason opened 1 year ago

alexgleason commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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