Skip to content

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

Open

Description

@alexgleason

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?

Activity

  1. crookse commented on Apr 7, 2023

    @crookse
    Member

    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.

  2. alexgleason commented on Apr 7, 2023

    @alexgleason
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions