GithubHelp home page GithubHelp logo

rmoorman / mint_web_socket Goto Github PK

View Code? Open in Web Editor NEW

This project forked from elixir-mint/mint_web_socket

0.0 2.0 0.0 211 KB

(Unofficial) WebSocket support for the Mint functional HTTP client

Home Page: https://hex.pm/packages/mint_web_socket

License: Apache License 2.0

Elixir 95.99% Shell 0.50% Erlang 3.50%

mint_web_socket's Introduction

Mint.WebSocket

CI Coverage Status hex.pm version hex.pm license Last Updated

(Unofficial) HTTP/1 and HTTP/2 WebSocket support for Mint ๐ŸŒฑ

Usage

Mint.WebSocket works together with Mint.HTTP API. For example, this snippet shows sending and receiving a text frame of "hello world" to a WebSocket server which echos our frames:

# bootstrap
{:ok, conn} = Mint.HTTP.connect(:http, "echo", 9000)

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

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

{:ok, conn, websocket} = Mint.WebSocket.new(conn, ref, status, resp_headers)

# send the hello world frame
{:ok, websocket, data} = Mint.WebSocket.encode(websocket, {:text, "hello world"})
{:ok, conn} = Mint.WebSocket.stream_request_body(conn, ref, data)

# receive the hello world reply frame
hello_world_echo_message = receive(do: (message -> message))
{:ok, conn, [{:data, ^ref, data}]} = Mint.WebSocket.stream(conn, hello_world_echo_message)
{:ok, websocket, [{:text, "hello world"}]} = Mint.WebSocket.decode(websocket, data)

What is Mint?

Mint is a functional HTTP/1 and HTTP/2 client library written in Elixir.

Why does it matter that it's functional? Isn't Elixir functional?

Existing WebSocket implementations like :gun / :websocket_client / Socket / WebSockex work by spawning and passing messages among processes. This is a very convenient interface in Elixir and Erlang, but it does not allow the author much control over the WebSocket connection.

Instead Mint.WebSocket is process-less: the entire HTTP and WebSocket states are kept in immutable data structures. This enables authors of WebSocket clients a more fine-grained control over the connections: Mint.WebSocket does not prescribe a process archicture.

For more information, check out Mint#Usage.

Spec conformance

This library aims to follow RFC6455 and RFC8441 as closely as possible and uses Autobahn|Testsuite to check conformance with every run of tests/CI. The auto-generated report produced by the Autobahn|Testsuite is uploaded on each push to main.

See the report here: https://mint-websocket.nyc3.digitaloceanspaces.com/autobahn/index.html

A Quick Note About HTTP/2

HTTP/2 WebSockets are not a built-in feature of HTTP/2. RFC8441 is an extension to the HTTP/2 protocol and server libraries are not obligated to implement it. In the current landscape, very few server libraries support the HTTP/2 extended CONNECT method which bootstraps WebSockets.

If Mint.WebSocket.upgrade/4 returns

{:error, conn, %Mint.WebSocketError{reason: :extended_connect_disabled}}

Then the server does not support HTTP/2 WebSockets or does not have them enabled.

Development workflow

Interested in developing Mint.WebSocket? The docker-compose.yml sets up an Elixir container, a simple websocket echo server, and the Autobahn|Testsuite fuzzing server.

(host)$ docker-compose up -d
(host)$ docker-compose exec app bash
(app)$ mix deps.get
(app)$ mix test
(app)$ iex -S mix

mint_web_socket's People

Contributors

the-mikedavis avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.