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

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement