good morning!!!!

Skip to content
Snippets Groups Projects
Select Git revision
  • 778d161bfd21f3cfa8052d8b84e8c86e08ce41c7
  • master default protected
  • danielle/type-error-return
  • fix-license-1
  • mafredri/build-add-makefile
  • mafredri/build-update-to-go1.22
  • mafredri/fix-ci-script-tool-version
  • dependabot/go_modules/internal/examples/internal-deps-4cadc2be3d
  • dependabot/go_modules/internal/examples/internal-deps-46eeb9c117
  • dependabot/go_modules/internal/examples/internal-deps-022ca1aea3
  • mafredri/chore-update-dependabot
  • dependabot/go_modules/internal/thirdparty/github.com/lesismal/nbio-1.5.12
  • dependabot/go_modules/internal/examples/golang.org/x/time-0.7.0
  • dependabot/go_modules/internal/thirdparty/github.com/lesismal/nbio-1.5.11
  • ethan/close-order
  • mafredri/chore-remove-funding
  • mafredri/fix-ci
  • dependabot/go_modules/internal/thirdparty/github.com/gobwas/ws-1.4.0
  • dependabot/go_modules/internal/thirdparty/github.com/gorilla/websocket-1.5.3
  • dependabot/go_modules/internal/thirdparty/github.com/lesismal/nbio-1.5.10
  • dependabot/go_modules/internal/thirdparty/github.com/gin-gonic/gin-1.10.0
  • v1.8.13
  • v1.8.12
  • v1.8.11
  • v1.8.10
  • v1.8.9
  • v1.8.8
  • v1.8.7
  • v1.8.6
  • v1.8.5
  • v1.8.4
  • v1.8.3
  • v1.8.2
  • v1.8.1
  • v1.8.0
  • v1.7.4
  • v1.7.3
  • v1.7.2
  • v1.7.1
  • v1.7.0
  • v1.6.5
41 results

websocket

  • Clone with SSH
  • Clone with HTTPS
  • user avatar
    Mathias Fredriksson authored and GitHub committed
    * build: update to Go 1.23
    * ci: update tools
    778d161b
    History

    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 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.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
    • No extra goroutine per connection to support cancellation with context.Context. This costs github.com/coder/websocket 2 KB of memory per connection.

    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.