You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- JSON and ProtoBuf helpers in the [wsjson](https://godoc.org/nhooyr.io/websocket/wsjson) and [wspb](https://godoc.org/nhooyr.io/websocket/wspb) subpackages
24
-
- Highly optimized by default
25
-
- Concurrent writes out of the box
26
-
-[Complete Wasm](https://godoc.org/nhooyr.io/websocket#hdr-Wasm) support
20
+
- Thorough tests, fully passes the WebSocket [autobahn-testsuite](https://github.com/crossbario/autobahn-testsuite)
- JSON and protobuf helpers in the [wsjson](https://godoc.org/nhooyr.io/websocket/wsjson) and [wspb](https://godoc.org/nhooyr.io/websocket/wspb) subpackages
For a production quality example that shows off the full API, see the [echo example on the godoc](https://godoc.org/nhooyr.io/websocket#example-package--Echo). On github, the example is at [example_echo_test.go](./example_echo_test.go).
37
-
38
-
Use the [errors.As](https://golang.org/pkg/errors/#As) function [new in Go 1.13](https://golang.org/doc/go1.13#error_wrapping) to check for [websocket.CloseError](https://godoc.org/nhooyr.io/websocket#CloseError).
39
-
There is also [websocket.CloseStatus](https://godoc.org/nhooyr.io/websocket#CloseStatus) to quickly grab the close status code out of a [websocket.CloseError](https://godoc.org/nhooyr.io/websocket#CloseError).
40
-
See the [CloseStatus godoc example](https://godoc.org/nhooyr.io/websocket#example-CloseStatus).
37
+
For a production quality example that demonstrates the complete API, see the [echo example](https://godoc.org/nhooyr.io/websocket#example-package--Echo).
41
38
42
39
### Server
43
40
@@ -84,98 +81,52 @@ if err != nil {
84
81
c.Close(websocket.StatusNormalClosure, "")
85
82
```
86
83
87
-
## Design justifications
88
-
89
-
- A minimal API is easier to maintain due to less docs, tests and bugs
90
-
- A minimal API is also easier to use and learn
91
-
- Context based cancellation is more ergonomic and robust than setting deadlines
92
-
- net.Conn is never exposed as WebSocket over HTTP/2 will not have a net.Conn.
93
-
- Using net/http's Client for dialing means we do not have to reinvent dialing hooks
94
-
and configurations like other WebSocket libraries
95
-
96
84
## Comparison
97
85
98
-
Before the comparison, I want to point out that both gorilla/websocket and gobwas/ws were
99
-
extremely useful in implementing the WebSocket protocol correctly so _big thanks_ to the
100
-
authors of both. In particular, I made sure to go through the issue tracker of gorilla/websocket
101
-
to ensure I implemented details correctly and understood how people were using WebSockets in
102
-
production.
103
-
104
86
### gorilla/websocket
105
87
106
-
https://github.com/gorilla/websocket
107
-
108
-
The implementation of gorilla/websocket is 6 years old. As such, it is
109
-
widely used and very mature compared to nhooyr.io/websocket.
110
-
111
-
On the other hand, it has grown organically and now there are too many ways to do
112
-
the same thing. Compare the godoc of
113
-
[nhooyr/websocket](https://godoc.org/nhooyr.io/websocket) with
114
-
[gorilla/websocket](https://godoc.org/github.com/gorilla/websocket) side by side.
115
-
116
-
The API for nhooyr.io/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
-
only 2200 lines whereas gorilla/websocket is at 3500 lines. That's more code to maintain,
119
-
more code to test, more code to document and more surface area for bugs.
120
-
121
-
Moreover, nhooyr.io/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
-
126
-
Some more advantages of nhooyr.io/websocket are that it supports concurrent writes and
127
-
makes it very easy to close the connection with a status code and reason. In fact,
128
-
nhooyr.io/websocket even implements the complete WebSocket close handshake for you whereas
129
-
with gorilla/websocket you have to perform it manually. See [gorilla/websocket#448](https://github.com/gorilla/websocket/issues/448).
130
-
131
-
The ping API is also nicer. gorilla/websocket requires registering a pong handler on the Conn
132
-
which results in awkward control flow. With nhooyr.io/websocket you use the Ping method on the Conn
133
-
that sends a ping and also waits for the pong.
134
-
135
-
Additionally, nhooyr.io/websocket can compile to [Wasm](https://godoc.org/nhooyr.io/websocket#hdr-Wasm) for the browser.
136
-
137
-
In terms of performance, the differences mostly depend on your application code. nhooyr.io/websocket
138
-
reuses message buffers out of the box if you use the wsjson and wspb subpackages.
139
-
As mentioned above, nhooyr.io/websocket also supports concurrent writers.
140
-
141
-
The WebSocket masking algorithm used by this package is also [1.75x](https://github.com/nhooyr/websocket/releases/tag/v1.7.4)
142
-
faster than gorilla/websocket or gobwas/ws while using only pure safe Go.
88
+
Advantages of [gorilla/websocket](https://github.com/gorilla/websocket):
143
89
144
-
The only performance con to nhooyr.io/websocket is that it uses one extra goroutine to support
145
-
cancellation with context.Context. This costs 2 KB of memory which is cheap compared to
Unmaintained and the API does not reflect WebSocket semantics. Should never be used.
153
-
154
-
See https://github.com/golang/go/issues/18152
155
-
156
-
### gobwas/ws
157
-
158
-
https://github.com/gobwas/ws
159
-
160
-
This library has an extremely flexible API but that comes at the cost of usability
161
-
and clarity.
162
-
163
-
This library is fantastic in terms of performance. The author put in significant
164
-
effort to ensure its speed and I have applied as many of its optimizations as
165
-
I could into nhooyr.io/websocket. Definitely check out his fantastic [blog post](https://medium.freecodecamp.org/million-websockets-and-go-cc58418460bb)
166
-
about performant WebSocket servers.
167
-
168
-
If you want a library that gives you absolute control over everything, this is the library.
169
-
But for 99.9% of use cases, nhooyr.io/websocket will fit better. It's nearly as performant
170
-
but much easier to use.
171
-
172
-
## Contributing
173
-
174
-
See [.github/CONTRIBUTING.md](.github/CONTRIBUTING.md).
175
-
176
-
## Users
177
-
178
-
If your company or project is using this library, feel free to open an issue or PR to amend this list.
179
-
180
-
-[Coder](https://github.com/cdr)
181
-
-[Tatsu Works](https://github.com/tatsuworks) - Ingresses 20 TB in websocket data every month on their Discord bot.
96
+
- Minimal and idiomatic API
97
+
- Compare godoc of [nhooyr.io/websocket](https://godoc.org/nhooyr.io/websocket) with [gorilla/websocket](https://godoc.org/github.com/gorilla/websocket) side by side.
- Gorilla writes directly to a net.Conn and so duplicates features of net/http.Client.
104
+
- Concurrent writes
105
+
- Close handshake ([gorilla/websocket#448](https://github.com/gorilla/websocket/issues/448))
106
+
- Idiomatic [ping pong](https://godoc.org/nhooyr.io/websocket#Conn.Ping) API
107
+
- Gorilla requires registering a pong callback before sending a Ping
108
+
- Can target Wasm ([gorilla/websocket#432](https://github.com/gorilla/websocket/issues/432))
109
+
- Transparent message buffer reuse with [wsjson](https://godoc.org/nhooyr.io/websocket/wsjson) and [wspb](https://godoc.org/nhooyr.io/websocket/wspb) subpackages
110
+
-[1.75x](https://github.com/nhooyr/websocket/releases/tag/v1.7.4) faster WebSocket masking implementation in pure Go
111
+
- Gorilla's implementation is slower and uses [unsafe](https://golang.org/pkg/unsafe/).
112
+
- Full [permessage-deflate](https://tools.ietf.org/html/rfc7692) compression extension support
113
+
- Gorilla only supports no context takeover mode
114
+
- Uses [klauspost/compress](https://github.com/klauspost/compress) for optimized compression
115
+
- See [gorilla/websocket#203](https://github.com/gorilla/websocket/issues/203)
0 commit comments