88
99(Unofficial) HTTP/1 and HTTP/2 WebSocket support for Mint 🌱
1010
11+ ## Usage
12+
13+ ` Mint.WebSocket ` works together with ` Mint.HTTP ` API. For example,
14+ this snippet shows sending and receiving a text frame of "hello world" to a
15+ WebSocket server which echos our frames:
16+
17+ ``` elixir
18+ # bootstrap
19+ {:ok , conn} = Mint .HTTP .connect (:http , " echo" , 9000 )
20+
21+ {:ok , conn, ref} = Mint .WebSocket .upgrade (:ws , conn, " /" , [])
22+
23+ http_get_message = receive (do: (message - > message))
24+ {:ok , conn, [{:status , ^ref , status}, {:headers , ^ref , resp_headers}, {:done , ^ref }]} =
25+ Mint .WebSocket .stream (conn, http_get_message)
26+
27+ {:ok , conn, websocket} = Mint .WebSocket .new (conn, ref, status, resp_headers)
28+
29+ # send the hello world frame
30+ {:ok , websocket, data} = Mint .WebSocket .encode (websocket, {:text , " hello world" })
31+ {:ok , conn} = Mint .WebSocket .stream_request_body (conn, ref, data)
32+
33+ # receive the hello world reply frame
34+ hello_world_echo_message = receive (do: (message - > message))
35+ {:ok , conn, [{:data , ^ref , data}]} = Mint .WebSocket .stream (conn, hello_world_echo_message)
36+ {:ok , websocket, [{:text , " hello world" }]} = Mint .WebSocket .decode (websocket, data)
37+ ```
38+
1139## What is Mint?
1240
1341Mint is a _ functional_ HTTP/1 and HTTP/2 client library written in Elixir.
1442
1543Why does it matter that it's functional? Isn't Elixir functional?
1644
1745Existing WebSocket implementations like
18- [ ` :gun ` ] ( https://github.com/ninenines/gun ) ,
19- [ ` :websocket_client ` ] ( https://github.com/jeremyong/websocket_client ) ,
20- or [ ` WebSockex ` ] ( https://github.com/Azolo/websockex ) work by spawning and
46+ [ ` :gun ` ] ( https://github.com/ninenines/gun ) /
47+ [ ` :websocket_client ` ] ( https://github.com/jeremyong/websocket_client ) /
48+ [ ` Socket ` ] ( https://github.com/meh/elixir-socket ) /
49+ [ ` WebSockex ` ] ( https://github.com/Azolo/websockex ) work by spawning and
2150passing messages among processes. This is a very convenient interface in
2251Elixir and Erlang, but it does not allow the author much control over
2352the WebSocket connection.
@@ -58,48 +87,6 @@ If `Mint.WebSocket.upgrade/4` returns
5887Then the server does not support HTTP/2 WebSockets or does not have them
5988enabled.
6089
61- Support for HTTP/2 extended CONNECT was added to Mint in version ` 1.4.0 ` .
62- If you need HTTP/2 support, make sure you require that version as a minimum.
63-
64- ``` elixir
65- # mix.exs
66- def deps do
67- [
68- {:mint_web_socket , " ~> 0.1" },
69- {:mint , " ~> 1.4" },
70- # ..
71- ]
72- end
73- ```
74-
75- ## Usage
76-
77- ` Mint.WebSocket ` piggybacks much of the existing ` Mint.HTTP ` API. For example,
78- this snippet shows sending and receiving a text frame of "hello world" to a
79- WebSocket server which echos our frames:
80-
81- ``` elixir
82- # bootstrap
83- {:ok , conn} = Mint .HTTP .connect (:http , " echo" , 9000 )
84-
85- {:ok , conn, ref} = Mint .WebSocket .upgrade (conn, " /" , [])
86-
87- http_get_message = receive (do: (message - > message))
88- {:ok , conn, [{:status , ^ref , status}, {:headers , ^ref , resp_headers}, {:done , ^ref }]} =
89- Mint .HTTP .stream (conn, http_get_message)
90-
91- {:ok , conn, websocket} = Mint .WebSocket .new (conn, ref, status, resp_headers)
92-
93- # send the hello world frame
94- {:ok , websocket, data} = Mint .WebSocket .encode (websocket, {:text , " hello world" })
95- {:ok , conn} = Mint .HTTP .stream_request_body (conn, ref, data)
96-
97- # receive the hello world reply frame
98- hello_world_echo_message = receive (do: (message - > message))
99- {:ok , conn, [{:data , ^ref , data}]} = Mint .HTTP .stream (conn, hello_world_echo_message)
100- {:ok , websocket, [{:text , " hello world" }]} = Mint .WebSocket .decode (websocket, data)
101- ```
102-
10390## Development workflow
10491
10592Interested in developing ` Mint.WebSocket ` ? The ` docker-compose.yml ` sets up
@@ -108,7 +95,7 @@ fuzzing server.
10895
10996```
11097(host)$ docker-compose up -d
111- (host)$ docker-compose exec app /bin/ bash
98+ (host)$ docker-compose exec app bash
11299(app)$ mix deps.get
113100(app)$ mix test
114101(app)$ iex -S mix
0 commit comments