55
66websocket is a minimal and idiomatic WebSocket library for Go.
77
8- This library is not final and the API is subject to change.
9-
108## Install
119
1210``` bash
13- go get nhooyr.io/websocket@v0.2 .0
11+ go get nhooyr.io/websocket@v1.0 .0
1412```
1513
1614## Features
1715
1816- Minimal and idiomatic API
19- - Tiny codebase at 1400 lines
17+ - Tiny codebase at 1700 lines
2018- First class context.Context support
2119- Thorough tests, fully passes the [ autobahn-testsuite] ( https://github.com/crossbario/autobahn-testsuite )
2220- Zero dependencies outside of the stdlib for the core library
2321- 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
2624
2725## Roadmap
2826
@@ -88,8 +86,9 @@ c.Close(websocket.StatusNormalClosure, "")
8886- net.Conn is never exposed as WebSocket over HTTP/2 will not have a net.Conn.
8987- Using net/http's Client for dialing means we do not have to reinvent dialing hooks
9088 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 )
9392
9493## Comparison
9594
@@ -111,7 +110,7 @@ Just compare the godoc of
111110
112111The API for nhooyr/websocket has been designed such that there is only one way to do things
113112which 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,
115114 more code to test, more code to document and more surface area for bugs.
116115
117116The 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
121120gorilla/websocket writes its handshakes to the underlying net.Conn which means
122121it has to reinvent hooks for TLS and proxies and prevents support of HTTP/2.
123122
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.
126135
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.
129140
130141### x/net/websocket
131142
0 commit comments