WebSocket library for a Phoenix application

I have a Phoenix application. I’m confused: which websocket library is recommended and most popular? I know of a few, which is de-facto? Or which one would you recommend?

Channels in Phoenix – isn’t what I’m looking for.

It is included in phoenix, You don’t need any package, You just need to configure…

Have a look here http://www.phoenixframework.org/docs/channels

1 Like

I don’t want channels.

I know, You want websocket… and channels are related

1 Like

Not really. Channels is something Phoenix built on top of websockets. It’s “related” the way controllers are related to HTTP.

You could always use Cowboy directly, which is what Phoenix abstracts / enhances. On the downside, it’s in erlang, so not that approachable, on the upside, you can look at the Phoenix source code for pointers on how to integrate + the Cowboy documentation is good.

1 Like

right, but my question is about websockets and not about abstructions on top on them.

1 Like

Sorry if I misunderstood, but your question is not yet clear to me…

If I was on node, I would choose socket.io, or something like this.

socket io use this channels terminology.

If I was on Rails, I would use actioncable (just kidding)

But if on phoenix, there is no need to additional library for websocket…

The websocket endpoint points to web/channels/user_socket.ex

If You want to create websocket endpoints in phoenix, You create … channels

And if You are coming from Rails world, just see them as real-time controller

forget about phoenix. you have elixir and you want to use websocket.

I’d go with cowboy as suggested before. We’ve been using it in production for about two years now (inside a phoenix application - we also didn’t want to use channels for few reasons) and it works perfectly. If you’re familiar with how the OTP works in general, you shouldn’t have any problems with setting it up because the documentation is really great.

https://ninenines.eu/docs/en/cowboy/1.0/guide/ws_handlers/

then why do we have multiple Elixir websocket libraries if someone uses Cowboy, someone else channels and nobody uses websocket libraries themselves?

Your answer is a bit like answering “How to plant an apple tree” with “Here, take this apple its already grown.”

@kaa.python wants to implement WS directly. Maybe he has some client that sends data over WS which is out of his control but he needs to fullfil its protocol which is tatally different to the way phioenix works? Maybe channels do introduce some additional transport overhead he wants to not have because he has to deal with hard bandwith limits? Perhaps he just wants to learn how WS works?


To answer the actual question.

I were not aware of any websocket “server” so far. But this question made me google a bit around, and I found one small library at hex which does not tell it were client only: https://hex.pm/packages/web_socket

This still needs plug though… I do hope it will help you anyway. If not you will probably need to dig into phoenix, how they do it.

1 Like

For my tests in WebSockex I essentially start a new cowboy server with a WS endpoint for every test. If you can figure out how to get into Phoenix’s routing rules and see how socket creates a new route in the dispatch method, then adding an endpoint that is just a WS handler from cowboy would be your best bet.

I just started using https://github.com/meh/elixir-socket to connect to a couchbase server’s changefeed. Works well so far, but I literally just got it connected and haven’t started worrying about handling disconnects or if the process that manages the websocket fails or anything. So I can’t elaborate too much.

1 Like

Do you want a websocket server or client?

Both…

A post was split to a new topic: Websocket event stream

For a raw websocket server I’d hook directly into cowboy. https://github.com/meh/elixir-socket gets a decent bit of usage I think as a client, but I’m not sure if it’s still the best available option.