|
4 | 4 | [](https://codecov.io/gh/nhooyr/ws)
|
5 | 5 | [](https://github.com/nhooyr/ws/releases)
|
6 | 6 |
|
7 |
| -ws is a clean and idiomatic WebSocket library for Go. |
| 7 | +ws is a minimal and idiomatic WebSocket library for Go. |
8 | 8 |
|
9 | 9 | This library is in heavy development.
|
10 | 10 |
|
11 | 11 | ## Install
|
12 | 12 |
|
13 | 13 | ```bash
|
14 |
| -go get nhooyr.io/ws |
| 14 | +go get nhooyr.io/ws@master |
15 | 15 | ```
|
16 | 16 |
|
17 |
| -## Why |
| 17 | +## Example |
| 18 | + |
| 19 | +### Server |
| 20 | + |
| 21 | +```go |
| 22 | +func main() { |
| 23 | + fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 24 | + c, err := ws.Accept(w, r, |
| 25 | + ws.AcceptSubprotocols("echo"), |
| 26 | + ) |
| 27 | + if err != nil { |
| 28 | + log.Printf("server handshake failed: %v", err) |
| 29 | + return |
| 30 | + } |
| 31 | + defer c.Close(ws.StatusInternalError, "") |
| 32 | + |
| 33 | + ctx, cancel := context.WithTimeout(r.Context(), time.Second*10) |
| 34 | + defer cancel() |
| 35 | + |
| 36 | + type myJsonStruct struct { |
| 37 | + MyField string `json:"my_field"` |
| 38 | + } |
| 39 | + err = wsjson.Write(ctx, c, myJsonStruct{ |
| 40 | + MyField: "foo", |
| 41 | + }) |
| 42 | + if err != nil { |
| 43 | + log.Printf("failed to write json struct: %v", err) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + c.Close(ws.StatusNormalClosure, "") |
| 48 | + }) |
| 49 | + // For production deployments, use a net/http.Server configured |
| 50 | + // with the appropriate timeouts. |
| 51 | + err := http.ListenAndServe("localhost:8080", fn) |
| 52 | + if err != nil { |
| 53 | + log.Fatalf("failed to listen and serve: %v", err) |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
18 | 57 |
|
19 |
| -There is no other Go WebSocket library with a clean API. |
| 58 | +### Client |
| 59 | + |
| 60 | +```go |
| 61 | +func main() { |
| 62 | + ctx := context.Background() |
| 63 | + ctx, cancel := context.WithTimeout(ctx, time.Minute) |
| 64 | + defer cancel() |
| 65 | + |
| 66 | + c, _, err := ws.Dial(ctx, "ws://localhost:8080") |
| 67 | + if err != nil { |
| 68 | + log.Fatalf("failed to ws dial: %v", err) |
| 69 | + } |
| 70 | + defer c.Close(ws.StatusInternalError, "") |
| 71 | + |
| 72 | + type myJsonStruct struct { |
| 73 | + MyField string `json:"my_field"` |
| 74 | + } |
| 75 | + err = wsjson.Write(ctx, c, myJsonStruct{ |
| 76 | + MyField: "foo", |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + log.Fatalf("failed to write json struct: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + c.Close(ws.StatusNormalClosure, "") |
| 83 | +} |
| 84 | +``` |
20 | 85 |
|
21 |
| -Comparisons with existing WebSocket libraries below. |
| 86 | +See [example_test.go](example_test.go) for more examples. |
22 | 87 |
|
23 |
| -### [x/net/websocket](https://godoc.org/golang.org/x/net/websocket) |
| 88 | +## Features |
24 | 89 |
|
| 90 | +- Full support of the WebSocket protocol |
| 91 | +- Simple to use because of the minimal API |
| 92 | +- Uses the context package for cancellation |
| 93 | +- Uses net/http's Client to do WebSocket dials |
| 94 | +- JSON and Protobuf helpers in wsjson and wspb subpackages |
| 95 | +- Compression extension is supported |
| 96 | +- Highly optimized |
| 97 | +- API will be ready for WebSockets over HTTP/2 |
25 | 98 |
|
26 |
| -Unmaintained and the API does not reflect WebSocket semantics. |
| 99 | +## Design considerations |
27 | 100 |
|
28 |
| -See https://github.com/golang/go/issues/18152 |
| 101 | +- Minimal API is easier to maintain and for others to learn |
| 102 | +- Context based cancellation is more ergonomic and robust than setting deadlines |
| 103 | +- No pings or pongs because TCP keep alives work fine for HTTP/1.1 and they do not make |
| 104 | + sense with HTTP/2 |
| 105 | +- net.Conn is never exposed as WebSocket's over HTTP/2 will not have a net.Conn. |
| 106 | +- Functional options make the API very clean and easy to extend |
| 107 | +- Compression is very useful for JSON payloads |
| 108 | +- Protobuf and JSON helpers make code terse |
| 109 | +- Using net/http's Client for dialing means we do not have to reinvent dialing hooks |
| 110 | + and configurations. Just pass in a custom net/http client if you want custom dialing. |
| 111 | + |
| 112 | +## Comparison |
29 | 113 |
|
30 | 114 | ### [gorilla/websocket](https://github.com/gorilla/websocket)
|
31 | 115 |
|
32 |
| -This package is the community standard but it is very old and over time |
| 116 | +This package is the community standard but it is very old and over timennn |
33 | 117 | has accumulated cruft. There are many ways to do the same thing and the API
|
34 |
| -overall is just not very clear. |
| 118 | +overall is just not very clear. Just compare the godoc of |
| 119 | +[nhooyr/ws](godoc.org/github.com/nhooyr/ws) side by side with |
| 120 | +[gorilla/websocket](godoc.org/github.com/gorilla/websocket). |
35 | 121 |
|
36 |
| -The callback hooks . The API for this library has been designed |
37 |
| -such that there is only one way to do things and callbacks have been avoided. |
| 122 | +The API for nhooyr/ws has been designed such that there is only one way to do things |
| 123 | +and with HTTP/2 in mind which makes using it correctly and safely much easier. |
38 | 124 |
|
39 |
| -Performance sensitive applications should use ws/wscore directly. |
| 125 | +### [x/net/websocket](https://godoc.org/golang.org/x/net/websocket) |
| 126 | + |
| 127 | +Unmaintained and the API does not reflect WebSocket semantics. Should never be used. |
| 128 | + |
| 129 | +See https://github.com/golang/go/issues/18152 |
40 | 130 |
|
41 | 131 | ### [gobwas/ws](https://github.com/gobwas/ws)
|
42 | 132 |
|
43 |
| -This library has an extremely flexible API but that comes at a cost of usability |
44 |
| -and clarity. Its just not clear and simple how to do things in a safe manner. |
| 133 | +This library has an extremely flexible API but that comes at the cost of usability |
| 134 | +and clarity. Its just not clear how to do things in a safe manner. |
45 | 135 |
|
46 |
| -## TODO |
| 136 | +This library is fantastic in terms of performance though. The author put in significant |
| 137 | +effort to ensure its speed and I have tried to apply as many of its teachings as |
| 138 | +I could into nhooyr/ws. |
47 | 139 |
|
48 |
| -- [ ] Fully implement. |
49 |
| -- [ ] Decide whether we want to do WebSocket pings by default maybe every 30s? |
50 |
| -- [ ] |
| 140 | +If you want a library that gives you absolute control over everything, this is the library, |
| 141 | +but for most users, the API provided by nhooyr/ws will definitely fit better as it will |
| 142 | +be just as performant but much easier to use. |
0 commit comments