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.

the-mikedavis

the-mikedavis

Finch and Req only do HTTP for now and I’m not sure if it’s in scope for those projects to add WebSocket support. Since the dependencies are compatible, you could use Mint.WebSocket in any project that already has Mint by adding it to your dependencies:

# mix.exs
def deps do
  [
    # ...
    {:mint, "~> 1.4"},
    {:mint_web_socket, "~> 1.0"}
  ]
end

and then using Mint.WebSocket directly. There’s an example WebSocket client implementation as a GenServer you can use for reference here. A high-level WebSocket client that wraps Mint.WebSocket like Finch wraps Mint would be pretty cool some day :smile:

ycastor

ycastor

Ohhhhhh nice! I’m using Mint and i’ll need that for WebSocket communications for my database driver, nice work!

Where Next?

Popular in Libraries Top

scohen
Lexical Lexical is a next-generation language server for the Elixir programming language. Features Context aware code completion As-you...
New
josevalim
Hi everyone, We would like to announce that Plataformatec is working on a new MySQL driver called MyXQL. Our goal is to eventually integ...
New
Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 13801 100
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
Crowdhailer
Raxx is an alternative to Plug and is inspired by projects such as Rack(Ruby) and Ring(Clojure). 1.0-rc.1 is now available. To use it re...
New
zoltanszogyenyi
Hey everyone :wave: Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-source U...
New
tompave
Hello there, I would like to share a feature toggles library (AKA feature flags) I’ve been working on. The main package is FunWithFlags...
New
Qqwy
Solution is a library to help you with working with ok/error-tuples in case and with-expressions by exposing special matching macros, as ...
New
Antrater
Hi everyone! I’m thrilled to announce a huge thing. We have been developing Elixir Moon Design System for quite a while. We are finally ...
New
bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
381 12391 119
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Sub Categories:

We're in Beta

About us Mission Statement