MCPcopy
hub / github.com/coder/websocket

github.com/coder/websocket @v1.8.15 sqlite

repository ↗ · DeepWiki ↗ · release v1.8.15 ↗
371 symbols 1,398 edges 56 files 117 documented · 32%
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:

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.

Extension points exported contracts — how you extend this code

WriterFunc (FuncType)
WriterFunc is used to implement one off io.Writers.
internal/util/util.go
ReaderFunc (FuncType)
ReaderFunc is used to implement one off io.Readers.
internal/util/util.go

Core symbols most depended-on inside this repo

Success
called by 86
internal/test/assert/assert.go
Close
called by 49
close.go
Equal
called by 49
internal/test/assert/assert.go
Write
called by 28
ws_js.go
CloseNow
called by 22
close.go
Dial
called by 18
dial.go
CloseRead
called by 18
read.go
Error
called by 17
close.go

Shape

Function 170
Method 150
Struct 39
TypeAlias 8
FuncType 3
Interface 1

Languages

Go99%
TypeScript1%

Modules by API surface

ws_js.go41 symbols
read.go26 symbols
conn.go22 symbols
write.go20 symbols
internal/wsjs/wsjs_js.go17 symbols
compress.go16 symbols
close.go16 symbols
accept.go15 symbols
internal/examples/chat/chat_test.go14 symbols
conn_test.go14 symbols
dial_test.go13 symbols
dial.go13 symbols

Dependencies from manifests, versioned

github.com/bytedance/sonicv1.11.6 · 1×
github.com/bytedance/sonic/loaderv0.1.1 · 1×
github.com/cloudwego/base64xv0.1.4 · 1×
github.com/cloudwego/iasmv0.2.0 · 1×
github.com/coder/websocketv0.0.0-0001010100000 · 1×
github.com/gabriel-vasile/mimetypev1.4.3 · 1×
github.com/gin-contrib/ssev0.1.0 · 1×
github.com/go-playground/localesv0.14.1 · 1×
github.com/go-playground/universal-translatorv0.18.1 · 1×
github.com/gobwas/httpheadv0.1.0 · 1×

For agents

$ claude mcp add websocket \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact