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!