hawkyre

hawkyre

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!

First 2 of 2 Posts! Switch mode

LostKobrakai

LostKobrakai

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.

larshei

larshei

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: https://gist.githubusercontent.com/LoganBresnahan/6ea9220c80024184340c2c93d3254b38/raw/06bc29b3be418c288a1df1b3585f8f0e8496330e/websocket_example_socket_handler.ex
  2. Docs for a cowboy websocket callback module: https://ninenines.eu/docs/en/cowboy/2.6/manual/cowboy_websocket/

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement