LegitStack

LegitStack

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:
https://github.com/SatoriNetwork/phx_raws

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?

Showing Posts 1 to 8

ruslandoga

ruslandoga

:waving_hand:

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.

josevalim

josevalim

Creator of Elixir

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

11
Post #2
RooSoft

RooSoft

Is there a way to make this thing listen to the root path, such as ws://localhost?

From what I understand, it listens to the /websocket path if the endpoint’s socket is configured like so socket "/", RelayWeb.Sockets.EchoSocket.

I’ve seen comments saying it’s better for whatever reason. Thing is, I have to fill a spec that requires that the websockets endpoint answers to the root path and I have no idea how to do that with Phoenix.Socket.Transport as we stand.

RooSoft

RooSoft

Also would like to know how to implement heartbeat such as sending a PING and expecting a PONG after a while instead of simply closing the connection after 60s of inactivity.

RooSoft

RooSoft

Figured out this part

socket("/", RelayWeb.Sockets.EchoSocket, websocket: [path: ""])
RooSoft

RooSoft

Also figured out…

In init/1,

Process.send_after(self(), :ping, ping_timeout)

Then, add a handle_info

  @impl true
  def handle_info(:ping, %{options: options} = state) do
    ping_timeout = Keyword.get(options, :ping)

    Process.send_after(self(), :ping, ping_timeout)

    {:push, {:ping, ""}, state}
  end

Will suggest the peer has to send a PONG, and if it does, that will count as a communication and thus will reset the timeout.

ku1ik

ku1ik

Thank you @RooSoft! I was trying to solve these 2 specific problems (path, ping) myself and your solutions to both work perfectly.

mtrudel

mtrudel

Creator of Bandit

Careful that sending pongs in response to client pings is explicitly handled for you by the underlying server in the WebSock world: WebSock — WebSock v0.5.3

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
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
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

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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews