Why I Built Another WebSocket Library
How fast and how lean can a WebSocket library in Go really be?
When I started building WebSocket apps, I wanted the best, not "good enough". The popular libraries work, and many people run them in production. But using them felt like working with a black box.
I called ReadMessage, and a message came out. How many frames did it arrive in? What did the handshake send? I couldn't easily see, and changing any of it was hard or impossible.
So I asked myself: can a library be easy to read, learn, and understand, without hiding any step from the people who use it?
I couldn't find one, so I wrote my own.
WLGOWS: The Efficiency Monster WebSocket Library for Go.
Built for speed and memory efficiency, without giving up correctness, and it lets you see and control almost every step.
Meet WLGOWS
Here is the core of an echo server. You run the handshake yourself, and the standard Listener does the rest:
// netConn and bufRW come from hijacking the HTTP request (net/http or Gin).
conn, _, err := wlgows.ServerHandShake(netConn, bufRW.Reader, bufRW.Writer, request, wlgows.ServerHandShakeConfig{})
if err != nil {
return
}
listener := conn.NewStandardListener() // ping, pong and close already answered
config := listener.GetConfig()
config.Text = func(frames wlgows.DataFrames) {
reader, err := conn.DataFramesReader(frames)
if err != nil {
listener.PauseListen(err) // Listen returns err
return
}
defer reader.Close()
message, err := io.ReadAll(wlgows.NewSizeLimitReader(reader, 4096))
if err != nil {
listener.PauseListen(err)
return
}
conn.SendText(message, wlgows.SendDataConfig{}) // echo
}
listener.SetConfig(config)
go conn.StartPingLoop(30*time.Second, nil) // heartbeat
listener.Listen()
Some error handling is trimmed here. The full, working version is in the README.
Even in this short example, you can see every step: the handshake, the frames of each message, the size limit and the ping interval. Nothing happens behind your back.
What you get
-
Safety and correctness first. wlgows follows RFC 6455 and RFC 7692, with 0 Autobahn failures as a server and as a client. It uses no
unsafe, and the tests cover 97.6% of statements. - Fast and lean. It is faster than gorilla/websocket, gobwas/ws and coder/websocket almost everywhere, and it uses under half the memory on reads. You'll see the numbers in the next section.
- Simple to learn. Learn wlgows, and you learn WebSocket. Each function does one clear step. No magic.
- Feature-rich. It has a server and a client, TLS, subprotocols, compression, streaming, keepalive and more.
- Well-documented. Want to go deeper? You can control almost everything, down to each frame, the I/O buffers and the raw connection. The guides show you how, in English and 繁體中文.
Benchmarks
I compared wlgows with gorilla/websocket, gobwas/ws and coder/websocket. wlgows is faster by up to:
| vs | Reads | Sends |
|---|---|---|
| gorilla | 3.3x | 1.8x |
| gobwas | 3.2x | 2.7x |
| coder | 4.0x | 7.8x |
It also uses under half the memory on every read, and 0 allocations on most sends.
Key cases
Lower is better.
| Case | wlgows | gorilla | gobwas | coder |
|---|---|---|---|---|
| Server send, one 1 MB frame | 102 ns | 176 ns | 124 ns | 287 ns |
| Client read, one 1 MB frame | 72 µs | 241 µs | 234 µs | 153 µs |
| Read memory, one 1 MB frame | 1.05 MB | 2.23 MB | 2.23 MB | 2.23 MB |
| Server send allocs, 8 frames of 16 KB | 0 | 16 | 8 | 0 |
| Client send, one 16 B frame | 120 ns | 93 ns | 112 ns | 328 ns |
The last row is the only kind of case where wlgows is slower, and that is on purpose. RFC 6455 says a client's masking key must be unpredictable, so wlgows takes it from crypto/rand. gorilla and gobwas use math/rand, which Go says is not for security work. On a tiny 16-byte message you can see that cost. From 1 KB up, wlgows is faster again.
Setup: Apple M5 · Go 1.26.6 · gorilla/websocket v1.5.3 · gobwas/ws v1.4.0 · coder/websocket v1.8.15 · the fastest run of each library. Send cases write to a connection that drops the bytes, so they measure each library's own work, not the network.
Every number is in BENCHMARK_COMPARISON.md, and you can run it yourself with the code in benchmarks/.
Why it's already v7, and why v7 is LTS
You may wonder why a library you have never heard of is already at v7.
wlgows started as my practice project. For a long time I was its only user, in my own private projects. Each time those projects needed something new, I changed the API, and it broke. So the version number went up fast. Honestly, I never thought it would last this long, or grow into something I'd share with the world.
At v6, I saw that wlgows already followed almost all of RFC 6455, and that it could be the fastest and leanest of the popular Go WebSocket libraries. So v7 is the last big rebuild, made for that goal.
That's why v7 is the long-term support version. No v8 is planned, at least not soon. Fixes and new features go into v7.
Is WLGOWS right for you?
A good fit if you:
- run busy servers where speed and memory matter
- want to see and understand every WebSocket step
- want low-level control: the handshake, each frame, your own buffer sizes
- prefer to make your own tradeoffs, not have the library make them for you
- need size limits or very large, streamed messages
Maybe not if you:
- want the least code possible, and speed or memory isn't a big concern
- can't use Go 1.24 or newer
Try it and tell me what breaks
go get github.com/weilun-shrimp/wlgows/v7
The Quick Start in the README is a working echo server you can run in a minute.
weilun-shrimp
/
wlgows
WLGOWS is a fast open source WebSocket library for Go. Built for speed and memory efficiency, without giving up correctness. WLGOWS 是高效能的開源 Go WebSocket 函式庫,為速度與記憶體效率而打造,且不犧牲正確性。
English · 繁體中文
WLGOWS
A fast open source WebSocket library for Go. Built for speed and memory efficiency, without giving up safety or correctness. Simple, well-tested and well-documented. It's faster than gorilla/websocket, gobwas/ws and coder/websocket in almost every benchmark It is quick to start with, and it does not hide how WebSocket works.
The Quick Start below is a working echo server.
Safety and correctness first. wlgows follows RFC 6455 and RFC 7692, with 0
Autobahn failures on both sides. We want
the fastest code and the least memory, but only when it stays safe
and correct: no unsafe, and no skipped bounds checks. Your own code may go
further if you know what you are doing. See
Faster and less memory.
Features
-
WebSocket Server — behind
net/httpor Gin, or on raw TCP when you know what you are doing -
WebSocket Client —
ws://andwss:// - …
wlgows was a one-person project for a long time. Now I want to hear from you. If something breaks, feels confusing or could be faster, please open an issue: https://github.com/weilun-shrimp/wlgows/issues
And if you like it, a ⭐ on GitHub helps other people find it. Thank you for reading!
Top comments (10)
building it because ReadMessage hid the frames and the handshake is the honest reason. a library you can read end to end beats one that just works.
Yes, that's my design idea too.
Developers should make their own tradeoffs, and a library shouldn't hide things from them.
Handling the low level is their choice: they can skip it, but the library shouldn't take that choice away.
WLGOWS Design Concept
Glad you feel the same!
letting the developer skip the low level without taking the choice away is the line. most libraries hide it and call it simple.
Agreed.
Hiding it can take away the chance to build a better app.
But they have their reasons.
Their target audience may be developers who just want it to work, junior developers, or people just starting to learn, and that's a fair choice too.
Still, great things take great effort.
For developers who want to build the best, I'll give them that chance.
picking the audience is the honest part. a library for people who just want it to work and one for people building the best are different libraries.
fair point on the junior devs who just want it to work. keeping the low level reachable for the ones who want to tune it is what keeps both groups happy.
The honest row in the benchmark table is the one where you lose — client send of a 16-byte frame at 120 ns against gorilla's 93 ns, because you pay for crypto/rand masking while they use math/rand. Most library authors would bury that row; the fact that you explain it as a deliberate security trade is what makes the rest of the numbers believable. The question it leaves me with: on a busy server pushing thousands of tiny frames — game ticks, chat, live cursors — does that masking cost stay a rounding error, or is there a message size where it starts to dominate the send path?
Thanks for reading it that closely!
The short answer is that a busy server never pays this cost.
RFC 6455 §5.1 says a server MUST NOT mask the frames it sends.
Only clients mask, so game ticks, chat and live cursors pushed from the server have no key to generate.
Here is my server side benchmarks comparison data:
On the client side, the cost is fixed per frame.
A crypto/rand key costs about 44 ns more than a math/rand one, whatever the frame size.
So it only dominates on tiny client frames.
At 1KB the payload already costs more than the key:
See: Full results and method
Thanks again for the attention!
That settles the server side — a busy server never pays the masking cost, and your tables show it: wlgows wins or ties every server row. The one row where you still lose is the 16-byte client send, and your own note explains why: the key cost is fixed per frame. Is the masking-key source the knob there — what does wlgows use to generate the key, and is that the next thing you'd tune to close the 16-byte gap?
I don't plan to tune that soon.
If I ever do, I'll only switch to a source that is as secure as
crypto/rand.RFC 6455 §5.3 says the masking key must be unpredictable and derived from a strong source of entropy.
In Go's standard library, crypto/rand is the source built for that.
math/randis documented as not for security-sensitive work.The README also promises that wlgows puts correctness first, and I intend to keep that promise.
I understand why gorilla and gobwas use
math/rand.The attack that masking blocks is proxy cache poisoning (RFC 6455 §10.3), and it is hard to pull off in practice, so it's a fair tradeoff for them.
I just choose the security side.
That costs about 27 ns on a 16-byte client frame, and the cost disappears from 1KB up.