the-mikedavis
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)
Trending in Announcing
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...
New
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub.
Docs are at OpenaiEx User Gu...
New
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
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly.
One of i...
New
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto.
pagination
cursor pagination
sorta...
New
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New
Other Trending Topics
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
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
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
Hey folks,
I just published a post about Hologram’s funding and where the project goes next - the short version:
Curiosum as Main Spons...
New
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
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!
stevensonmt
This looks cool. Pros/Cons vs Phoenix?
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 testPhoenix.Channels (see here).ycastor
Ohhhhhh nice! I’m using Mint and i’ll need that for WebSocket communications for my database driver, nice work!
thojanssens1
Thank you!
We also have finch built on top of mint
and req built on top of finch
How would one use WebSockets when depending on any of those two other libs?
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:
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
thojanssens1
What would also be nice is to have a similar lib for SSE Server Sent Events. Which seem to be the future with http2 support.
bfolkens
We started using this in production last month and it’s significantly faster than WebSockex. I’ll try to run some more formal perf tests when I get a chance.
bfolkens
Extracted the basics today, it’s up now at https://hex.pm/packages/wind. Any feedback welcome, of course. Will add more soon.
bunopnu
I am currently working on a WebSocket client which is similar to WebSockex but built atop Mint ecosystem: GitHub - bunopnu/fresh: WebSocket client for Elixir, built atop the Mint ecosystem · GitHub
Waiting for feedbacks…