v7 · LTS

WLGOWS A fast open source WebSocket library for Go.

Built for speed and memory efficiency, without giving up safety or correctness. Unlock your WebSocket app's full potential.

go get github.com/weilun-shrimp/wlgows/v7

Why choose us

Safety and correctness first

RFC 6455 and 7692, with 0 Autobahn failures on both sides. No unsafe.

Autobahn results →

Fast and lean

vs gorilla, gobwas and coder: faster almost everywhere. Under half the memory on reads.

Benchmarks →

Simple to learn

Learn wlgows, and you learn WebSocket. Each function does one clear step. No magic.

Quick Start →

Well-documented

Guides for every part, in English and 繁體中文.

Guides →

Feature-rich

Everything a WebSocket app needs, on both sides.

  • WebSocket Server

    Behind net/http or Gin, or on raw TCP

  • WebSocket Client

    ws:// and wss://.

  • TLS/SSL

    On both sides.

  • Subprotocols

    Offer, pick or require one, on either side.

  • HTTP Hijacking

    From http.Server or Gin.

  • Listener

    A read loop that validates against RFC 6455 and routes frames to your hooks.

  • Frame-level control

    Build and send your own frames when you need to.

  • Streaming

    Send or read a message larger than memory, frame by frame.

  • Compression

    permessage-deflate (RFC 7692), on both sides.

  • Memory in your hands

    Pick each connection's read and write buffer, as small as you need.

  • Memory protection

    Cap how much a peer can send, before and after decompressing.

  • Keepalive

    Ping on an interval, with a payload you choose per ping.

  • Concurrent sending

    Lock-guarded, and control frames are never stuck behind a long message.

  • And even more

    See the Guides →

Design concept

You control almost everything, from high level to low level.

wlgows never decides for you.

I believe development should be free, not locked down by the library. Developers should make their own tradeoffs.

Almost every function is a helper for one step of WebSocket. wlgows itself is built from these same helpers, at every level. So you can always do any step yourself, the same way wlgows does, and you always know what is going on.

  1. Keepalive pings
  2. Each message
  3. Each frame
  4. The handshake
  5. I/O buffers
  6. The raw connection

And even more. See the Guides →

Benchmarks

Compared with gorilla/websocket, gobwas/ws and coder/websocket

Speed

Faster than coder in every case but two ties, and faster than gorilla and gobwas almost everywhere.

vs gorilla faster by up to

Reads
3.3x
Sends
1.8x

vs gobwas faster by up to

Reads
3.2x
Sends
2.7x

vs coder faster by up to

Reads
4.0x
Sends
7.8x
Server send, one 1 MB frame ns per op · lower is better
wlgows 102 ns
gorilla 176 ns
gobwas 124 ns
coder 287 ns
Client read, one 1 MB frame µs per op · lower is better
wlgows 72 µs
gorilla 241 µs
gobwas 234 µs
coder 153 µs

Only 16B client sends are slower, 1.1x to 1.3x, on purpose. wlgows takes masking keys from a secure random source, unlike gorilla and gobwas, because safety and correctness come first.

Memory

Under half the memory on every read, and 0 allocs on most sends.

Under half the bytes per read of any of the three
Read memory, one 1 MB frame MB per op · lower is better
wlgows 1.05 MB
gorilla 2.23 MB
gobwas 2.23 MB
coder 2.23 MB
Server send allocs, 8 frames of 16 KB allocs per op · lower is better
wlgows 0
gorilla 16
gobwas 8
coder 0

Apple M5 · Go 1.26.6 · gorilla/websocket v1.5.3 · gobwas/ws v1.4.0 · coder/websocket v1.8.15 · optimal run of each library

Every number →

Well-tested

97.6% statement coverage
0 Autobahn failures as a server
0 Autobahn failures as a client
Autobahn TestSuite: wlgows v7, wstest 25.10.1, all 517 cases on each side
Side OK Non-Strict Informational Unimplemented Failed
Server 510 4 3 0 0
Client 510 4 3 0 0
go test -race ./...

Quick Start

A working echo server, and a client to talk to it.

main.go
package main

import (
	"io"
	"log"
	"net/http"

	"github.com/weilun-shrimp/wlgows/v7"
)

// readMessage returns the bytes of one message. It decompresses a compressed
// message and checks text as UTF-8. See READING.md.
func readMessage(conn *wlgows.Conn, frames wlgows.DataFrames) ([]byte, error) {
	reader, err := conn.DataFramesReader(frames)
	if err != nil {
		return nil, err
	}
	defer reader.Close() // always close it
	return io.ReadAll(reader)
}

func main() {
	log.Fatal(http.ListenAndServe(":8001", http.HandlerFunc(handle)))
}

func handle(w http.ResponseWriter, r *http.Request) {
	hijacker, ok := w.(http.Hijacker)
	if !ok {
		http.Error(w, "could not hijack connection", http.StatusInternalServerError)
		return
	}
	netConn, bufRW, err := hijacker.Hijack()
	if err != nil {
		http.Error(w, "could not hijack connection", http.StatusInternalServerError)
		return
	}
	defer netConn.Close()

	// Pass bufRW.Reader: net/http may have read the start of the first frame into it.
	conn, _, err := wlgows.ServerHandShake(netConn, bufRW.Reader, bufRW.Writer, r, wlgows.ServerHandShakeConfig{})
	if err != nil {
		return
	}

	// Ping, pong, close and reserved opcodes already answered.
	listener := conn.NewStandardListener()

	// SetConfig replaces all of it, so start from what the standard hooks left.
	config := listener.GetConfig()
	config.Text = func(frames wlgows.DataFrames) {
		message, err := readMessage(conn, frames)
		if err != nil {
			listener.PauseListen(err) // Listen returns err
			return
		}
		conn.SendText(message, wlgows.SendDataConfig{}) // echo
	}
	listener.SetConfig(config)

	log.Println("closing:", listener.Listen())
}

A short, runnable server. For production, follow the docs to pick your own limits and settings. The full server →

main.go
package main

import (
	"bufio"
	"io"
	"log"

	"github.com/weilun-shrimp/wlgows/v7"
)

// readMessage returns the bytes of one message. It decompresses a compressed
// message and checks text as UTF-8. See READING.md.
func readMessage(conn *wlgows.Conn, frames wlgows.DataFrames) ([]byte, error) {
	reader, err := conn.DataFramesReader(frames)
	if err != nil {
		return nil, err
	}
	defer reader.Close() // always close it
	return io.ReadAll(reader)
}

func main() {
	// Dial only connects and builds the request. You run the handshake
	// yourself, so you can still add headers to `request` before it is sent.
	netConn, request, err := wlgows.Dial("ws://localhost:8001", nil)
	if err != nil {
		panic(err)
	}
	defer netConn.Close()

	// The reader and the writer take 4096 bytes/conn each; see READING.md and SENDING.md to shrink them.
	conn, _, err := wlgows.ClientHandShake(netConn, bufio.NewReader(netConn), bufio.NewWriter(netConn), request, wlgows.ClientHandShakeConfig{})
	if err != nil {
		panic(err)
	}

	listener := conn.NewStandardListener()

	config := listener.GetConfig() // what the standard hooks left
	config.Text = func(frames wlgows.DataFrames) {
		message, err := readMessage(conn, frames)
		if err != nil {
			listener.PauseListen(err) // Listen returns err
			return
		}
		log.Printf("received: %s", message)
	}
	listener.SetConfig(config)

	conn.SendText([]byte("Hello, WebSocket!"), wlgows.SendDataConfig{})

	log.Println("closing:", listener.Listen())
}

A short, runnable client. For production, follow the docs to pick your own limits and settings. The full client →