5
5
6
6
websocket is a minimal and idiomatic WebSocket library for Go.
7
7
8
- This library is not final and the API is subject to change.
9
-
10
8
## Install
11
9
12
10
``` bash
13
- go get nhooyr.io/websocket@v0.2 .0
11
+ go get nhooyr.io/websocket@v1.0 .0
14
12
```
15
13
16
14
## Features
17
15
18
16
- Minimal and idiomatic API
19
- - Tiny codebase at 1400 lines
17
+ - Tiny codebase at 1700 lines
20
18
- First class context.Context support
21
19
- Thorough tests, fully passes the [ autobahn-testsuite] ( https://github.com/crossbario/autobahn-testsuite )
22
20
- Zero dependencies outside of the stdlib for the core library
23
21
- JSON and ProtoBuf helpers in the wsjson and wspb subpackages
24
- - High performance
25
- - Concurrent reads and writes out of the box
22
+ - Highly optimized by default
23
+ - Concurrent writes out of the box
26
24
27
25
## Roadmap
28
26
@@ -88,8 +86,9 @@ c.Close(websocket.StatusNormalClosure, "")
88
86
- net.Conn is never exposed as WebSocket over HTTP/2 will not have a net.Conn.
89
87
- Using net/http's Client for dialing means we do not have to reinvent dialing hooks
90
88
and configurations like other WebSocket libraries
91
- - We do not support the compression extension because Go's compress/flate library is very memory intensive
92
- and browsers do not handle WebSocket compression intelligently. See [ #5 ] ( https://github.com/nhooyr/websocket/issues/5 )
89
+ - We do not support the deflate compression extension because Go's compress/flate library
90
+ is very memory intensive and browsers do not handle WebSocket compression intelligently.
91
+ See [ #5 ] ( https://github.com/nhooyr/websocket/issues/5 )
93
92
94
93
## Comparison
95
94
@@ -111,7 +110,7 @@ Just compare the godoc of
111
110
112
111
The API for nhooyr/websocket has been designed such that there is only one way to do things
113
112
which makes it easy to use correctly. Not only is the API simpler, the implementation is
114
- only 1400 lines whereas gorilla/websocket is at 3500 lines. That's more code to maintain,
113
+ only 1700 lines whereas gorilla/websocket is at 3500 lines. That's more code to maintain,
115
114
more code to test, more code to document and more surface area for bugs.
116
115
117
116
The future of gorilla/websocket is also uncertain. See [ gorilla/websocket #370 ] ( https://github.com/gorilla/websocket/issues/370 ) .
@@ -121,11 +120,23 @@ also uses net/http's Client and ResponseWriter directly for WebSocket handshakes
121
120
gorilla/websocket writes its handshakes to the underlying net.Conn which means
122
121
it has to reinvent hooks for TLS and proxies and prevents support of HTTP/2.
123
122
124
- Some more advantages of nhooyr/websocket are that it supports concurrent reads,
125
- writes and makes it very easy to close the connection with a status code and reason.
123
+ Some more advantages of nhooyr/websocket are that it supports concurrent writes and
124
+ makes it very easy to close the connection with a status code and reason.
125
+
126
+ nhooyr/websocket also responds to pings, pongs and close frames in a separate goroutine so that
127
+ your application doesn't always need to read from the connection unless it expects a data message.
128
+ gorilla/websocket requires you to constantly read from the connection to respond to control frames
129
+ even if you don't expect the peer to send any messages.
130
+
131
+ In terms of performance, the differences depend on your application code. nhooyr/websocket
132
+ reuses buffers efficiently out of the box if you use the wsjson and wspb subpackages whereas
133
+ gorilla/websocket does not. As mentioned above, nhooyr/websocket also supports concurrent
134
+ writers out of the box.
126
135
127
- In terms of performance, the only difference is nhooyr/websocket is forced to use one extra
128
- goroutine for context.Context support. Otherwise, they perform identically.
136
+ The only performance con to nhooyr/websocket is that uses two extra goroutines. One for
137
+ reading pings, pongs and close frames async to application code and another to support
138
+ context.Context cancellation. This costs 4 KB of memory which is cheap compared
139
+ to the benefits.
129
140
130
141
### x/net/websocket
131
142
0 commit comments