coder
websocket
Go

Minimal and idiomatic WebSocket library for Go

Last updated Jul 8, 2026
5.3k
Stars
366
Forks
67
Issues
+16
Stars/day
Attention Score
89
Language breakdown
No language data available.
Files click to expand
README

websocket

Go Reference Go Coverage

websocket is a minimal and idiomatic WebSocket library for Go.

Install

go get github.com/coder/websocket
[!NOTE]
Coder now maintains this project as explained in this blog post.
We're grateful to nhooyr for authoring and maintaining this project from
2019 to 2024.

Highlights

Roadmap

See GitHub issues for minor issues but the major future enhancements are:

  • [ ] Perfect examples #217
  • [ ] wstest.Pipe for in memory testing #340
  • [ ] Ping pong heartbeat helper #267
  • [ ] Ping pong instrumentation callbacks #246
  • [ ] Graceful shutdown helpers #209
  • [ ] Assembly for WebSocket masking #16
- WIP at #326, about 3x faster
  • [ ] HTTP/2 #4
  • [ ] The holy grail #402

Examples

For a production quality example that demonstrates the complete API, see the echo example.

For a full stack example, see the chat example.

Server

http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
	c, err := websocket.Accept(w, r, nil)
	if err != nil {
		// ...
	}
	defer c.CloseNow()

// Set the context as needed. Use of r.Context() is not recommended // to avoid surprising behavior (see http.Hijacker). ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) defer cancel()

var v any err = wsjson.Read(ctx, c, &v) if err != nil { // ... }

log.Printf("received: %v", v)

c.Close(websocket.StatusNormalClosure, "") })

Client

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil) if err != nil { // ... } defer c.CloseNow()

err = wsjson.Write(ctx, c, "hi") if err != nil { // ... }

c.Close(websocket.StatusNormalClosure, "")

Comparison

gorilla/websocket

Advantages of gorilla/websocket:

Advantages of github.com/coder/websocket:
  • Minimal and idiomatic API
- Compare godoc of github.com/coder/websocket with gorilla/websocket side by side. - Will enable easy HTTP/2 support in the future - Gorilla writes directly to a net.Conn and so duplicates features of net/http.Client. - Gorilla requires registering a pong callback before sending a Ping - Gorilla's implementation is slower and uses unsafe. Soon we'll have assembly and be 3x faster #326 - Gorilla only supports no context takeover mode

golang.org/x/net/websocket

golang.org/x/net/websocket is deprecated. See golang/go/issues/18152.

The net.Conn can help in transitioning to github.com/coder/websocket.

gobwas/ws

gobwas/ws has an extremely flexible API that allows it to be used in an event driven style for performance. See the author's blog post.

However it is quite bloated. See https://pkg.go.dev/github.com/gobwas/ws

When writing idiomatic Go, github.com/coder/websocket will be faster and easier to use.

lesismal/nbio

lesismal/nbio is similar to gobwas/ws in that the API is event driven for performance reasons.

However it is quite bloated. See https://pkg.go.dev/github.com/lesismal/nbio

When writing idiomatic Go, github.com/coder/websocket will be faster and easier to use.

🔗 More in this category

© 2026 GitRepoTrend · coder/websocket · Updated daily from GitHub