good morning!!!!

Skip to content
Snippets Groups Projects
Select Git revision
  • 0596e7a0e19d842ac2d9db0d598902dba6485cc4
  • master default protected
  • 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
  • dependabot/go_modules/internal/examples/golang.org/x/time-0.6.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
    Anmol Sethi authored
    [qrvnl@dios ~/src/websocket] 130$ go test -bench=. ./wsjson/
    goos: linux
    goarch: amd64
    pkg: nhooyr.io/websocket/wsjson
    cpu: 12th Gen Intel(R) Core(TM) i5-1235U
    BenchmarkJSON/json.Encoder/8-12         14041426            72.59 ns/op  110.21 MB/s          16 B/op          1 allocs/op
    BenchmarkJSON/json.Encoder/16-12        13936426            86.99 ns/op  183.92 MB/s          16 B/op          1 allocs/op
    BenchmarkJSON/json.Encoder/32-12        11416401           115.3 ns/op   277.59 MB/s          16 B/op          1 allocs/op
    BenchmarkJSON/json.Encoder/128-12        4600574           264.7 ns/op   483.55 MB/s          16 B/op          1 allocs/op
    BenchmarkJSON/json.Encoder/256-12        2710398           433.9 ns/op   590.06 MB/s          16 B/op          1 allocs/op
    BenchmarkJSON/json.Encoder/512-12        1588930           717.3 ns/op   713.82 MB/s          16 B/op          1 allocs/op
    BenchmarkJSON/json.Encoder/1024-12        823138          1484 ns/op     689.80 MB/s          16 B/op          1 allocs/op
    BenchmarkJSON/json.Encoder/2048-12        402823          2875 ns/op     712.32 MB/s          16 B/op          1 allocs/op
    BenchmarkJSON/json.Encoder/4096-12        213926          5602 ns/op     731.14 MB/s          16 B/op          1 allocs/op
    BenchmarkJSON/json.Encoder/8192-12         92864         11281 ns/op     726.19 MB/s          16 B/op          1 allocs/op
    BenchmarkJSON/json.Encoder/16384-12        39318         29203 ns/op     561.04 MB/s          19 B/op          1 allocs/op
    BenchmarkJSON/json.Marshal/8-12         10768671           114.5 ns/op    69.89 MB/s          48 B/op          2 allocs/op
    BenchmarkJSON/json.Marshal/16-12        10140996           113.9 ns/op   140.51 MB/s          64 B/op          2 allocs/op
    BenchmarkJSON/json.Marshal/32-12         9211780           121.6 ns/op   263.06 MB/s          64 B/op          2 allocs/op
    BenchmarkJSON/json.Marshal/128-12        4632796           264.2 ns/op   484.53 MB/s         224 B/op          2 allocs/op
    BenchmarkJSON/json.Marshal/256-12        2441511           473.5 ns/op   540.65 MB/s         432 B/op          2 allocs/op
    BenchmarkJSON/json.Marshal/512-12        1298788           896.2 ns/op   571.27 MB/s         912 B/op          2 allocs/op
    BenchmarkJSON/json.Marshal/1024-12        602084          1866 ns/op     548.83 MB/s        1808 B/op          2 allocs/op
    BenchmarkJSON/json.Marshal/2048-12        341151          3817 ns/op     536.61 MB/s        3474 B/op          2 allocs/op
    BenchmarkJSON/json.Marshal/4096-12        175594          7034 ns/op     582.32 MB/s        6548 B/op          2 allocs/op
    BenchmarkJSON/json.Marshal/8192-12         83222         15023 ns/op     545.30 MB/s       13591 B/op          2 allocs/op
    BenchmarkJSON/json.Marshal/16384-12        33087         39348 ns/op     416.39 MB/s       27304 B/op          2 allocs/op
    PASS
    ok      nhooyr.io/websocket/wsjson  32.934s
    0596e7a0
    History

    websocket

    Go Reference Go Coverage

    websocket is a minimal and idiomatic WebSocket library for Go.

    Install

    go get nhooyr.io/websocket

    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
    • 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()
    
    	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.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 nhooyr.io/websocket 2 KB of memory per connection.

    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 it is quite bloated. See https://pkg.go.dev/github.com/gobwas/ws

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