GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - coder/websocket: Minimal and idiomatic WebSocket library for Go

BranchesTags
Open more actions menu

Latest commit

 

History

798 Commits
798 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
ci
ci
 
 
internal
internal
 
 
wsjson
wsjson
 
 
.gitignore
.gitignore
 
 
AGENTS.md
AGENTS.md
 
 
LICENSE.txt
LICENSE.txt
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
accept.go
accept.go
 
 
accept_test.go
accept_test.go
 
 
autobahn_test.go
autobahn_test.go
 
 
close.go
close.go
 
 
close_shared.go
close_shared.go
 
 
close_test.go
close_test.go
 
 
compress.go
compress.go
 
 
compress_test.go
compress_test.go
 
 
conn.go
conn.go
 
 
conn_test.go
conn_test.go
 
 
dial.go
dial.go
 
 
dial_test.go
dial_test.go
 
 
doc.go
doc.go
 
 
errors.go
errors.go
 
 
example_test.go
example_test.go
 
 
export_test.go
export_test.go
 
 
frame.go
frame.go
 
 
frame_test.go
frame_test.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
hijack.go
hijack.go
 
 
hijack_go120_test.go
hijack_go120_test.go
 
 
main_test.go
main_test.go
 
 
mask.go
mask.go
 
 
mask_amd64.s
mask_amd64.s
 
 
mask_arm64.s
mask_arm64.s
 
 
mask_asm.go
mask_asm.go
 
 
mask_asm_test.go
mask_asm_test.go
 
 
mask_go.go
mask_go.go
 
 
mask_test.go
mask_test.go
 
 
netconn.go
netconn.go
 
 
netconn_js.go
netconn_js.go
 
 
netconn_notjs.go
netconn_notjs.go
 
 
read.go
read.go
 
 
stringer.go
stringer.go
 
 
write.go
write.go
 
 
ws_js.go
ws_js.go
 
 
ws_js_test.go
ws_js_test.go
 
 

Repository files navigation

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

  • Minimal and idiomatic API
  • First class context.Context support
  • Fully passes the WebSocket autobahn-testsuite
  • Zero dependencies
  • JSON helpers in the wsjson subpackage
  • Zero alloc reads and writes
  • Concurrent writes
  • Close handshake
  • net.Conn wrapper
  • Ping pong API
  • RFC 7692 permessage-deflate compression
  • CloseRead helper for write only connections
  • Compile to Wasm

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:

  • Mature and widely used
  • Prepared writes
  • Configurable buffer sizes

Advantages of github.com/coder/websocket:

  • Minimal and idiomatic API
    • Compare godoc of github.com/coder/websocket with gorilla/websocket side by side.
  • net.Conn wrapper
  • Zero alloc reads and writes (gorilla/websocket#535)
  • Full context.Context support
  • Dial uses net/http.Client
    • Will enable easy HTTP/2 support in the future
    • Gorilla writes directly to a net.Conn and so duplicates features of net/http.Client.
  • Concurrent writes
  • Close handshake (gorilla/websocket#448)
  • Idiomatic ping pong API
    • Gorilla requires registering a pong callback before sending a Ping
  • Can target Wasm (gorilla/websocket#432)
  • Transparent message buffer reuse with wsjson subpackage
  • 1.75x faster WebSocket masking implementation in pure Go
    • Gorilla's implementation is slower and uses unsafe. Soon we'll have assembly and be 3x faster #326
  • Full permessage-deflate compression extension support
    • Gorilla only supports no context takeover mode
  • CloseRead helper for write only connections (gorilla/websocket#492)

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.

About

Minimal and idiomatic WebSocket library for Go

Topics

gohttp2idiomaticminimalwebsocket

Resources

Readme
ISC license

Code of conduct

Code of conduct

Security policy

Security policy
Activity
Custom properties

Stars

5.6k stars

Watchers

48 watching

Forks

390 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.