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)
end

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:
https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html#module-websocket-support

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: https://gist.githubusercontent.com/LoganBresnahan/6ea9220c80024184340c2c93d3254b38/raw/06bc29b3be418c288a1df1b3585f8f0e8496330e/websocket_example_socket_handler.ex
  2. Docs for a cowboy websocket callback module: Nine Nines: cowboy_websocket(3)