Websocket server in Elixir or phoenix

I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that smart I thought I’d go as high level as possible.

I found this thread:

and this one:

and forked his code to:

I as I was bringing it up to date I ran into this issue:

Phoenix.Socket.Transport.connect/6 is undefined or private. Did you mean:

 * connect_info/3Elixir

Call to missing or unexported function ‘Elixir.Phoenix.Socket.Transport’:connect/6ElixirLS Dialyzer

on this line:
case Transport.connect(endpoint, handler, transport, __MODULE__, nil, conn.params) do

so after consulting the docs, which I don’t understand I came up with this, which I;m sure is wildly incorrect as well:

case Transport.connect(
          %{:endpoint=>endpoint, :transport=>transport, :params=>conn.params, :options=>[
            handler: handler, __MODULE__: __MODULE__, serializer: nil]}) do

But it told me I shouldn’t use this function anyway:

Phoenix.Socket.Transport.connect/1 is undefined or private. Did you mean:

 * connect_info/3Elixir

Call to missing or unexported function ‘Elixir.Phoenix.Socket.Transport’:connect/1ElixirLS Dialyzer

I think the code is almost updated, but what do I have to do to get it to a working state?

2 Likes

:wave:

Using cowboy might be easier than you think. Here’s a small example of how it could be done. The important bits are the plug child spec and the module implementing cowboy_websocket behaviour. You can ignore the Web.UserSocket and “pubsub” specifics.

More info on cowboy_websocket behaviour.

2 Likes

There is a simpler API in Phoenix for custom websockets, you can follow the example here:

https://hexdocs.pm/phoenix/Phoenix.Socket.Transport.html#module-example

5 Likes