Logan

Logan

Basic Websocket Chat Example Using the Cowboy Server w/o Phoenix

Here is an example of a Mix application that utilizes Cowboy to handle websocket connections. If anyone has an idea about making this work with multiple nodes that would be helpful for the question posed in the comments. Thanks.
https://medium.com/@loganbbres/elixir-websocket-chat-example-c72986ab5778

First 10 of 10 Posts Switch mode

idi527

idi527

:waving_hand:

Instead of

file = EEx.eval_file("lib/application.html.eex")

you might want to compile the template to a function

require EEx
EEx.function_from_file(:defp, :index_html, "lib/application.html.eex", [])

get "/" do
  send_resp(conn, :ok, index_html())
end

it would help you avoid reevaluating the template on each request.


For distribution, there are several options worth exploring, you can go the “phoenix route” and make a basic pubsub or you can go “riak route” and use a hash ring to distribute chat processes across nodes. The former is simpler, the latter is less chatty.

Logan

Logan OP

Thanks for the tip of function_from_file. I will update the post using it.

As far as distribution goes I found a comment from a post that said Phoenix uses the :pg2 module from Erlang. I quickly checked it out and found that you can easily setup a distributed PubSub with it!

Somewhere in your app you would need to register a group :pg2.create("Global")

Then, in my case, I would add pids to this group with :pg2.join("Global", self()), then when sending messages to these pids you would get them all with :pg2.get_members("Global") which grabs all pids from all the connected nodes, and finally send them a message with something like Process.send\3

The Elixir and Erlang ecosystem makes it so easy sometimes :smile:

darnahsan

darnahsan

Thank you for this excellent post, helped me setup my PoC , much appreciated.

kasvith

kasvith

this is really valuble

kasvith

kasvith

@Logan is there any way we can detect disconnected websocket connection with this approach? When a user disconnected we need to inform chat room that user is offline

Logan

Logan OP

Hi @kasvith were you able to find a solution to your issue? Cowboy does implement a terminate/3 callback. Check out, Nine Nines: cowboy_websocket(3).

When you implement the callback, you will have access to self() which will be the process that belongs to the client that disconnected.

Sorry for the late reply and thanks again for checking out the article.

kasvith

kasvith

aha, thank you for pointing out. It will be great if you can add this to the article as well

kasvith

kasvith

Hi Logan, Is there any idiomatic way we can parse the qs of request in cowboy init handler?

Logan

Logan OP

Hi kasvith,

Elixir has the URI module that includes a function for parsing a query string.
https://hexdocs.pm/elixir/1.12/URI.html#decode_query/3

URI.decode_query(request.qs)
kasvith

kasvith

thank you

— All posts loaded —

Where Next?

Trending in Guides/Tuts Top

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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement