andarotajo

andarotajo

How do I build a WebSocket client library?

Hello, I’m very new to Elixir and looking to get started on a little project I’m interested in. Inspired by this blog post I’d like to build a “simple” client library giving users a simpler way to use Home Assistant’s WebSocket API.

I think a library like this give users the most flexibility in how they write automations and keeps the project scope limited for my own sanity :slight_smile:

But since Elixir and writing libraries in general are very new to me, I’d love to get some pointers on how to go about that. Things I’ve been wondering:

  • I thought about building the library around callbacks, e.g. a callback for Home Assistant’s state changes, allowing the user to pattern match what they need. How do I build a WebSocket client around this concept
  • Is using a GenServer the way to go? I’d need to keep a state
  • Besides the URL there’s also a token I’d need to connect to Home Assistant. Is there a “best practice” on how to configure stuff like this?

Some good libraries I can use as guidance for code quality and stuff would probably help if you’ve got some suggestions.

For reference, this is Home Assistant’s WebSocket API

Thank you very much for helping!

Most Liked

jstimps

jstimps

I like to use gun for client-side ws work. A while ago I wrote a little Livebook example to remind my future self of the function calls involved.

bbangert

bbangert

I recently extracted a websocket lib for HA that I published on hex: Hassock — hassock v0.1.3

This uses the newer HA command that uses diff’s for efficiency with an optional ETS cache for faster queries without sending everything over the websocket connection.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement