5
5
6
6
websocket is a minimal and idiomatic WebSocket library for Go.
7
7
8
- > ** note** : I haven't been responsive for questions/reports on the issue tracker but I do
9
- > read through and there are no outstanding bugs. There are certainly some nice to haves
10
- > that I should merge in/figure out but nothing critical. I haven't given up on adding new
11
- > features and cleaning up the code further, just been busy. Should anything critical
12
- > arise, I will fix it.
13
-
14
8
## Install
15
9
16
- ``` bash
10
+ ``` sh
17
11
go get nhooyr.io/websocket
18
12
```
19
13
@@ -23,18 +17,23 @@ go get nhooyr.io/websocket
23
17
- First class [ context.Context] ( https://blog.golang.org/context ) support
24
18
- Fully passes the WebSocket [ autobahn-testsuite] ( https://github.com/crossbario/autobahn-testsuite )
25
19
- [ Zero dependencies] ( https://pkg.go.dev/nhooyr.io/websocket?tab=imports )
26
- - JSON and protobuf helpers in the [ wsjson] ( https://pkg.go.dev/nhooyr.io/websocket/wsjson ) and [ wspb ] ( https://pkg.go.dev/nhooyr.io/websocket/wspb ) subpackages
20
+ - JSON helpers in the [ wsjson] ( https://pkg.go.dev/nhooyr.io/websocket/wsjson ) subpackage
27
21
- Zero alloc reads and writes
28
22
- Concurrent writes
29
23
- [ Close handshake] ( https://pkg.go.dev/nhooyr.io/websocket#Conn.Close )
30
24
- [ net.Conn] ( https://pkg.go.dev/nhooyr.io/websocket#NetConn ) wrapper
31
25
- [ Ping pong] ( https://pkg.go.dev/nhooyr.io/websocket#Conn.Ping ) API
32
26
- [ RFC 7692] ( https://tools.ietf.org/html/rfc7692 ) permessage-deflate compression
27
+ - [ CloseRead] ( https://pkg.go.dev/nhooyr.io/websocket#Conn.CloseRead ) helper for write only connections
33
28
- Compile to [ Wasm] ( https://pkg.go.dev/nhooyr.io/websocket#hdr-Wasm )
34
29
35
30
## Roadmap
36
31
32
+ - [ ] Ping pong heartbeat helper [ #267 ] ( https://github.com/nhooyr/websocket/issues/267 )
33
+ - [ ] Graceful shutdown helper [ #209 ] ( https://github.com/nhooyr/websocket/issues/209 )
34
+ - [ ] Assembly for WebSocket masking [ #16 ] ( https://github.com/nhooyr/websocket/issues/16 )
37
35
- [ ] HTTP/2 [ #4 ] ( https://github.com/nhooyr/websocket/issues/4 )
36
+ - [ ] The holy grail [ #402 ] ( https://github.com/nhooyr/websocket/issues/402 )
38
37
39
38
## Examples
40
39
@@ -51,7 +50,7 @@ http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
51
50
if err != nil {
52
51
// ...
53
52
}
54
- defer c.Close (websocket. StatusInternalError , " the sky is falling " )
53
+ defer c.CloseNow ( )
55
54
56
55
ctx , cancel := context.WithTimeout (r.Context (), time.Second *10 )
57
56
defer cancel ()
@@ -78,7 +77,7 @@ c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)
78
77
if err != nil {
79
78
// ...
80
79
}
81
- defer c.Close (websocket. StatusInternalError , " the sky is falling " )
80
+ defer c.CloseNow ( )
82
81
83
82
err = wsjson.Write (ctx, c, " hi" )
84
83
if err != nil {
@@ -110,12 +109,14 @@ Advantages of nhooyr.io/websocket:
110
109
- Gorilla writes directly to a net.Conn and so duplicates features of net/http.Client.
111
110
- Concurrent writes
112
111
- Close handshake ([ gorilla/websocket #448 ] ( https://github.com/gorilla/websocket/issues/448 ) )
112
+ - [ CloseRead] ( https://pkg.go.dev/nhooyr.io/websocket#Conn.CloseRead ) helper for write only connections
113
113
- Idiomatic [ ping pong] ( https://pkg.go.dev/nhooyr.io/websocket#Conn.Ping ) API
114
114
- Gorilla requires registering a pong callback before sending a Ping
115
115
- Can target Wasm ([ gorilla/websocket #432 ] ( https://github.com/gorilla/websocket/issues/432 ) )
116
- - Transparent message buffer reuse with [ wsjson] ( https://pkg.go.dev/nhooyr.io/websocket/wsjson ) and [ wspb ] ( https://pkg.go.dev/nhooyr.io/websocket/wspb ) subpackages
116
+ - Transparent message buffer reuse with [ wsjson] ( https://pkg.go.dev/nhooyr.io/websocket/wsjson ) subpackage
117
117
- [ 1.75x] ( https://github.com/nhooyr/websocket/releases/tag/v1.7.4 ) faster WebSocket masking implementation in pure Go
118
118
- Gorilla's implementation is slower and uses [ unsafe] ( https://golang.org/pkg/unsafe/ ) .
119
+ Soon we'll have assembly and be 4.5x faster [ #326 ] ( https://github.com/nhooyr/websocket/pull/326 )
119
120
- Full [ permessage-deflate] ( https://tools.ietf.org/html/rfc7692 ) compression extension support
120
121
- Gorilla only supports no context takeover mode
121
122
- [ CloseRead] ( https://pkg.go.dev/nhooyr.io/websocket#Conn.CloseRead ) helper ([ gorilla/websocket #492 ] ( https://github.com/gorilla/websocket/issues/492 ) )
0 commit comments