Logan
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
Trending in Guides/Tuts
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
Instead of
you might want to compile the template to a function
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
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
:pg2module 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 likeProcess.send\3The Elixir and Erlang ecosystem makes it so easy sometimes
darnahsan
Thank you for this excellent post, helped me setup my PoC , much appreciated.
kasvith
this is really valuble
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
Hi @kasvith were you able to find a solution to your issue? Cowboy does implement a
terminate/3callback. 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
aha, thank you for pointing out. It will be great if you can add this to the article as well
kasvith
Hi Logan, Is there any idiomatic way we can parse the qs of request in cowboy init handler?
Logan
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
kasvith
thank you