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!

First 4 of 4 Posts Switch mode

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.

andarotajo

andarotajo OP

Thanks for the heads up! To be honest I didn’t touch Elixir much after experimenting around a bit but this project is still on my list for when I finally learn Elixir.

Looks great on first glance. Have you tried writing an automation with it or plan to?

bbangert

bbangert

Currently I use this library on a bit of a test project to control a LoupeDeck Live device ( GitHub - bbangert/loupey: Elixir library for Loupedeck devices · GitHub ). I’ll eventually bake this into a Nerves project and add Elgato StreamDeck support, so that its easy to have stand-alone physical Home Assistant controls and displays with a bit more powerful software backing it than what ESPHome can do.
My Loupedeck mainly acts as a display and control for HA, so I can turn the volume knob to change the volume of my audio deck, tap a touch slider to change the light levels that HA controls via Phillips Hue, and I have some buttons that just display sensor states.

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

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