hawkyre

hawkyre

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!

Showing Posts 1 to 2

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/
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews