Safety and correctness first
RFC 6455 and 7692, with 0 Autobahn failures on both sides. No unsafe.
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
RFC 6455 and 7692, with 0 Autobahn failures on both sides. No unsafe.
vs gorilla, gobwas and coder: faster almost everywhere. Under half the memory on reads.
Benchmarks →Learn wlgows, and you learn WebSocket. Each function does one clear step. No magic.
Quick Start →Guides for every part, in English and 繁體中文.
Guides →Everything a WebSocket app needs, on both sides.
Behind net/http or Gin, or on raw TCP
ws:// and wss://.
On both sides.
Offer, pick or require one, on either side.
From http.Server or Gin.
A read loop that validates against RFC 6455 and routes frames to your hooks.
Build and send your own frames when you need to.
Send or read a message larger than memory, frame by frame.
permessage-deflate (RFC 7692), on both sides.
Pick each connection's read and write buffer, as small as you need.
Cap how much a peer can send, before and after decompressing.
Ping on an interval, with a payload you choose per ping.
Lock-guarded, and control frames are never stuck behind a long message.
See the Guides →
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.
Compared with gorilla/websocket, gobwas/ws and coder/websocket
Faster than coder in every case but two ties, and faster than gorilla and gobwas almost everywhere.
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.
Under half the memory on every read, and 0 allocs on most sends.
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
| Side | OK | Non-Strict | Informational | Unimplemented | Failed |
|---|---|---|---|---|---|
| Server | 510 | 4 | 3 | 0 | 0 |
| Client | 510 | 4 | 3 | 0 | 0 |
go test -race ./...
A working echo server, and a client to talk to it.
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 →
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 →
Full programs, with the production setup. Pick the one closest to your app, and make it yours.
Echo server behind net/http, via http.Hijacker.
Echo server behind Gin, via c.Writer.Hijack(). GET /ping keeps answering JSON alongside the WebSocket.
Echo server over raw TCP, with wlgows.Run and Accept. TLS comes from crypto/tls.
Interactive client. Type a line to send it, exit to close cleanly. Works with all three servers.
Receives a streamed message frame by frame, straight to disk.
StreamingStreams a file as one binary message, a chunk at a time.
Accepting a connection, the handshake and its refusals, hijacking from net/http or Gin, TLS, subprotocols, compression.
Dialing, TLS, the handshake, adding headers, subprotocols, compression.
Size limits, errors and close codes, compressed messages, large messages, the read buffer.
The standard Listener, hooks, limits, errors, noticing a dead peer, pausing.
Whole messages, streaming, compression, control frames, pinging on an interval, the write buffer, locks.
Each config field, context takeover, memory, compressing large data yourself.