Forwarder module for websocket routes with cowboy

I have a module that forwards different routes to different route handlers for normal API routes, which works with plug. It’s something like this:

defmodule LLServer.Web.Router do
  use Plug.Router

  # Forwards

  alias LLServer.Web.Router.Auth, as: AuthRouter
  forward("/auth", to: AuthRouter)

  alias LLServer.Web.Router.Admin, as: AdminRouter
  forward("/admin", to: AdminRouter)

  alias LLServer.Web.Router.User, as: UserRouter
  forward("/user", to: UserRouter)

Now I need to integrate websockets into the server as well, and I’ve decided to try doing it with cowboy via implementing the :cowboy_websocket behaviour. However, I’ll probably have multiple websocket connections for different parts of the website, so I’d like to forward different websocket routes to different websocket handlers. Is there any easy and clean way to do so without putting it inside the dispatch option for the Plug.Cowboy app?

I’ve tried doing the same as above for websockets as such:

defmodule LLServer.Web.WebsocketRouter do
  use Plug.Router

  # Forwards

  alias LLServer.Web.Websockets.HubChat, as: HubChatWS
  forward("/hub/:hub_id/chat", to: HubChatWS)

But I get an error saying init only has 1 argument, while the websocket implementation has 2. Plus, this uses plug, which isn’t really cowboy. Another option I’ve considered is using regexes inside the first websocket to determine which one to use but I feel like that’ll cause some problems since you’re technically already inside a websocket when the was handler’s init is called.

Another option is that I’m just approaching websockets badly and I should just have one connection for the whole website and just send different messages depending on the action that was performed, but that seems like it gets really cluttered really fast.

Any insight on this would be really welcome!

Plug 1.14. afaik can route websocket requests and has built in support for websocket updates, so you no longer need to manually integrate with the underlying webserver used with plug.

1 Like

Thanks for the hint, @LostKobrakai, I was just avout to ask a similar question.

I think here is where more information can be found:

I have not yet implemented this, just used the cowboy socket directly so far.

@hawkyre On the router level your websocket request is just a normal http request.
Every websocket connection starts with a simple HTTP request and is then upgraded to a websocket connection. See the docs above on how to tell Plug to upgrade the connection.

More useful information:

  1. An example implementation of a cowboy websocket handler module:
  2. Docs for a cowboy websocket callback module: Nine Nines: cowboy_websocket(3)