Skip to content
Minimal and idiomatic WebSocket library for Go
Go Shell Dockerfile
Branch: master
Clone or download

Latest commit

nhooyr Merge pull request #238 from nhooyr/dev
Fix deadlock introduced in v1.8.5
Latest commit 02861b4 May 10, 2020

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Back to GH Actions May 10, 2020
ci Fix deadlock introduced in v1.8.5 May 10, 2020
examples Merge pull request #225 from nhooyr/undep May 10, 2020
internal Allow passing http:// and https:// URLS to Dial Apr 14, 2020
wsjson Less strict about message type of JSON payloads May 5, 2020
wspb Remove dependency on golang.org/x/xerrors Feb 16, 2020
.gitignore Add conn benchmark Feb 13, 2020
LICENSE.txt Fix capitalization on contributing.md May 30, 2019
README.md Merge pull request #237 from nhooyr/ci May 10, 2020
accept.go Patch Accept for Gin May 10, 2020
accept_js.go Allow passing http:// and https:// URLS to Dial Apr 14, 2020
accept_test.go Fix for CI Apr 14, 2020
autobahn_test.go Fix negotation of flate parameters Feb 16, 2020
close.go Remove dependency on golang.org/x/xerrors Feb 16, 2020
close_notjs.go Fix bad close handshake logic Apr 14, 2020
close_test.go Use basic test assertions Feb 13, 2020
compress.go Disable safari compression compatability layer Apr 14, 2020
compress_notjs.go Fix negotation of flate parameters Feb 16, 2020
compress_test.go Simplify sliding window API Feb 13, 2020
conn.go Minor cleanup Feb 12, 2020
conn_notjs.go Remove Grace partially Apr 14, 2020
conn_test.go Patch Accept for Gin May 10, 2020
dial.go Doc fixes Apr 14, 2020
dial_test.go Fix negotation of flate parameters Feb 16, 2020
doc.go Update docs and random little issues Feb 16, 2020
example_test.go Improve docs and fix examples Apr 14, 2020
export_test.go Report how efficient compression is in BenchmarkConn Feb 16, 2020
frame.go Remove dependency on golang.org/x/xerrors Feb 16, 2020
frame_test.go Switch to klauspost/compress Feb 16, 2020
go.mod Patch Accept for Gin May 10, 2020
go.sum Patch Accept for Gin May 10, 2020
netconn.go Remove dependency on golang.org/x/xerrors Feb 16, 2020
read.go Clarify CloseRead docs Apr 14, 2020
stringer.go Change websocket to WebSocket in docs/errors Nov 29, 2019
write.go Fix deadlock introduced in v1.8.5 May 10, 2020
ws_js.go Remove grace for now Apr 14, 2020
ws_js_test.go Use basic test assertions Feb 13, 2020

README.md

websocket

godoc coverage

websocket is a minimal and idiomatic WebSocket library for Go.

Install

go get nhooyr.io/websocket

Highlights

Roadmap

  • HTTP/2 #4

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.Close(websocket.StatusInternalError, "the sky is falling")

	ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
	defer cancel()

	var v interface{}
	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.Close(websocket.StatusInternalError, "the sky is falling")

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

c.Close(websocket.StatusNormalClosure, "")

Comparison

gorilla/websocket

Advantages of gorilla/websocket:

Advantages of nhooyr.io/websocket:

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 nhooyr.io/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 when writing idiomatic Go, nhooyr.io/websocket will be faster and easier to use.

You can’t perform that action at this time.