the-mikedavis

the-mikedavis

Mint.WebSocket - (unofficial) WebSocket support for Mint 🌱

Mint.WebSocket (github) (hex) is a library built on the awesome Mint (github) (hex) functional HTTP client.

Mint.WebSocket extends Mint’s process-less architecture with a handful of functions for upgrading connections to the WebSocket protocol and encoding and decoding WebSocket frames. For example, we can send and receive some frames from an echo WebSocket server:

# see https://websocket.org/echo.html
{:ok, conn} = Mint.HTTP.connect(:https, "echo.websocket.org", 443)

{:ok, conn, ref} = Mint.WebSocket.upgrade(conn, "/", [])

message = receive(do: (message -> message))
{:ok, conn, [{:status, ^ref, status}, {:headers, ^ref, resp_headers}, {:done, ^ref}]} =
  Mint.HTTP.stream(conn, message)
{:ok, conn, websocket} = Mint.WebSocket.new(conn, ref, status, resp_headers)

{:ok, websocket, data} =
  Mint.WebSocket.encode(websocket, {:text, "Rock it with Mint.WebSocket"})
{:ok, conn} = Mint.HTTP.stream_request_body(conn, ref, data)

message = receive(do: (message -> message))
{:ok, conn, [{:data, ^ref, data}]} = Mint.HTTP.stream(conn, message)
{:ok, websocket, [{:text, "Rock it with Mint.WebSocket"}]} =
  Mint.WebSocket.decode(websocket, data)

{:ok, websocket, data} = Mint.WebSocket.encode(websocket, :close)
{:ok, conn} = Mint.HTTP.stream_request_body(conn, ref, data)

message = receive(do: (message -> message))
{:ok, conn, [{:data, ^ref, data}]} = Mint.HTTP.stream(conn, message)
{:ok, websocket, [{:close, 1_000, ""}]} =
  Mint.WebSocket.decode(websocket, data)

Mint.HTTP.close(conn)

Features:

  • HTTP/1 and HTTP/2 support through the same API
    • although HTTP/2 WebSocket support is currently very limited among server frameworks
  • support for Extensions
    • the popular “permessage-deflate” extension is included in the library
  • conformance checks with the Autobahn|Testsuite are built in to the CI
  • performance is roughly on par with :gun (Autobahn|Testsuite comparison)

Most Liked

the-mikedavis

the-mikedavis

v1.0.0 was just released!

Since the last update, the API has been refactored to depend cleanly on Mint so that Mint.WebSocket should be future-proof for new Mint releases. We’ve also been using Mint.WebSocket extensively in production over the last (almost) year. And when I say extensively, I mean extensively: my metrics say that in the last week we made nearly one million connections with Mint.WebSocket!

the-mikedavis

the-mikedavis

Mint.WebSocket is a WebSocket client so you could use it to, for example, connect to a cowboy server or a Phoenix.Channel (Slipstream actually uses Mint.WebSocket under the hood). In fact, Phoenix itself recently switched to using Mint.WebSocket to test Phoenix.Channels (see here).

bfolkens

bfolkens

Extracted the basics today, it’s up now at https://hex.pm/packages/wind. Any feedback welcome, of course. Will add more soon.

Where Next?

Popular in Announcing Top

josevalim
Hello everyone, We have just released NimbleCSV which is a small and fast CSV parsing library for Elixir. It allows developers to define...
New
Hal9000
Here is my first stab at this. README pasted below. https://github.com/Hal9000/elixir_random Comments and critiques are welcome. Thank...
New
Crowdhailer
I have been updating a library that allows you to pipe between functions that use the erlang result tuple convention. Assuming you have ...
New
bryanjos
Hi, I just published version 0.23.0 of Elixirscript. https://github.com/bryanjos/elixirscript/blob/master/CHANGELOG.md Most of the chan...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
ityonemo
Currently just starting out on a new mini-project - getting zig NIFs to run in elixir. https://github.com/ityonemo/zigler The idea here...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
kevinlang
Hey all, We have made an Ecto3 Adapter for SQLite3, ecto_sqlite3! We have successfully on-boarded the full suite of integration tests (...
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

We're in Beta

About us Mission Statement