@@ -110,34 +110,33 @@ https://github.com/gorilla/websocket
110
110
This package is the community standard but it is 6 years old and over time
111
111
has accumulated cruft. There are too many ways to do the same thing.
112
112
Just compare the godoc of
113
- [ nhooyr/websocket] ( https://godoc.org/nhooyr.io/websocket ) side by side with
114
- [ gorilla/websocket] ( https://godoc.org/github.com/gorilla/websocket ) .
113
+ [ nhooyr/websocket] ( https://godoc.org/nhooyr.io/websocket ) with
114
+ [ gorilla/websocket] ( https://godoc.org/github.com/gorilla/websocket ) side by side .
115
115
116
- The API for nhooyr/websocket has been designed such that there is only one way to do things
117
- which makes it easy to use correctly. Not only is the API simpler, the implementation is
116
+ The API for nhooyr/websocket has been designed such that there is only one way to do things.
117
+ This makes it easy to use correctly. Not only is the API simpler, the implementation is
118
118
only 2200 lines whereas gorilla/websocket is at 3500 lines. That's more code to maintain,
119
119
more code to test, more code to document and more surface area for bugs.
120
120
121
- Moreover, nhooyr/websocket has support for newer Go idioms such as context.Context and
122
- also uses net/http's Client and ResponseWriter directly for WebSocket handshakes.
123
- gorilla/websocket writes its handshakes to the underlying net.Conn which means
124
- it has to reinvent hooks for TLS and proxies and prevents support of HTTP/2.
121
+ Moreover, nhooyr/websocket supports newer Go idioms such as context.Context.
122
+ It also uses net/http's Client and ResponseWriter directly for WebSocket handshakes.
123
+ gorilla/websocket writes its handshakes to the underlying net.Conn.
124
+ Thus it has to reinvent hooks for TLS and proxies and prevents support of HTTP/2.
125
125
126
126
Some more advantages of nhooyr/websocket are that it supports concurrent writes and
127
127
makes it very easy to close the connection with a status code and reason.
128
128
129
129
The ping API is also nicer. gorilla/websocket requires registering a pong handler on the Conn
130
130
which results in awkward control flow. With nhooyr/websocket you use the Ping method on the Conn
131
- that sends a ping and also waits for the pong, though you must be reading from the connection
132
- for the pong to be read.
131
+ that sends a ping and also waits for the pong.
133
132
134
133
Additionally, nhooyr.io/websocket can compile to [ WASM] ( https://godoc.org/nhooyr.io/websocket#hdr-WASM ) for the browser.
135
134
136
135
In terms of performance, the differences mostly depend on your application code. nhooyr/websocket
137
136
reuses message buffers out of the box if you use the wsjson and wspb subpackages.
138
137
As mentioned above, nhooyr/websocket also supports concurrent writers.
139
138
140
- The only performance con to nhooyr/websocket is that uses one extra goroutine to support
139
+ The only performance con to nhooyr/websocket is that it uses one extra goroutine to support
141
140
cancellation with context.Context. This costs 2 KB of memory which is cheap compared to
142
141
the benefits.
143
142
@@ -161,9 +160,9 @@ effort to ensure its speed and I have applied as many of its optimizations as
161
160
I could into nhooyr/websocket. Definitely check out his fantastic [ blog post] ( https://medium.freecodecamp.org/million-websockets-and-go-cc58418460bb )
162
161
about performant WebSocket servers.
163
162
164
- If you want a library that gives you absolute control over everything, this is the library,
165
- but for most users, the API provided by nhooyr/websocket will fit better as it is nearly just
166
- as performant but much easier to use correctly and idiomatic .
163
+ If you want a library that gives you absolute control over everything, this is the library.
164
+ But for 99.9% of use cases, nhooyr/websocket will fit better. It's nearly as performant
165
+ but much easier to use.
167
166
168
167
## Contributing
169
168
0 commit comments