andarotajo

andarotajo

How to structure a library around a WebSocket API?

I’ve been getting into Elixir recently and now that I’ve finished the “Elixir in Action” book I’m ready to dive into my first project: a library around a specific WebSocket API.

Since I’ve never built a library in any language I’d like to get some pointers how to do this in a meaningful way.

WebSocket library
I think using an existing WebSocket library to handle the connection etc. would be a good start? I’ve been looking around and didn’t find any that’s “actively” developed. Do you have any recommendations? I’ve been looking at WebSockex and Fresh

User friendly structure
How do I go about structuring this? Fresh has various options when starting it up with start_link. Do I hide those except for the URI the user has to provide or just reference them in the documentation so the user can decide? There’s also an authentication needed via access token so I’d need the user to provide this to the state.

The API I’m trying to write the wrapper for has states and events you can listen for. My idea was to have a module handling the WebSocket callbacks and pass the relevant data to the main module where I’d have callbacks that the user implements. Something like this:

defmodule Lib.WebSocket do
    use Fresh/WebSockex/...

    @impl Fresh/WebSockex/...
    def handle_in({:text, frame}, state) do
        case Jason.decode(frame) do
            {:ok, msg} -> handle_msg(msg, state)
            {:error, error} -> {:ok, state}
        end
    end

    def handle_msg(%{"type" => "auth_required"}, state) do
        # get access token from state and send back?
    end

    def handle_msg(%{"type" => "state_changed", ...}, state) do
        # pass to main module
        Lib.handle_state(entity, from, to)
    end
end

I’ve not yet worked with callbacks but I’d have handle_state be the callback so the user can pattern match to whatever combinaion of state change they want. Is that a good way to do it?

Error handling is another part I’m unsure about

So many questions, hope you can help me out here :pray:

Most Liked

sodapopcan

sodapopcan

Ha, you’ll likely be interested in this very recent thread as well as this one.

Also, welcome to Elixir Forum!

sodapopcan

sodapopcan

Ah sorry I didn’t mean to imply that it would answer all of your questions, it was just recent discussion on the same topic I thought you would may be able to get something out of. Sorry that it was no help :frowning:

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement