DEV Community

Cover image for WLGOWS: The Efficiency Monster WebSocket Library for Go
weilun-shrimp
weilun-shrimp

Posted on

WLGOWS: The Efficiency Monster WebSocket Library for Go

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

🌐 Website · ⭐ GitHub

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()
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The Quick Start in the README is a working echo server you can run in a minute.

GitHub logo 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

Website Go Reference Autobahn Master CI Statement coverage

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/http or Gin, or on raw TCP when you know what you are doing
  • WebSocket Client — ws:// and wss://
  • …

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)

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

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.

Collapse
 
weilunshrimp profile image
weilun-shrimp •

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!

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

letting the developer skip the low level without taking the choice away is the line. most libraries hide it and call it simple.

Thread Thread
 
weilunshrimp profile image
weilun-shrimp •

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.

Thread Thread
 
robertpaschal profile image
Odinaka Robert Nnamani •

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.

Thread Thread
 
robertpaschal profile image
Odinaka Robert Nnamani •

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.

Collapse
 
userxnom profile image
Abdullah Naseer •

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?

Collapse
 
weilunshrimp profile image
weilun-shrimp •

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:

Server send (ns/op) wlgows gorilla gobwas coder
One-16B 35(tie) 46 34 219
Many64-16B 1765 2910 2067 13764
One-1KB 53 69 59 237

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:

Client send (ns/op) wlgows gorilla
One-16B 120 93
One-1KB 173 187
One-16KB 933 1596

See: Full results and method

Thanks again for the attention!

Collapse
 
userxnom profile image
Abdullah Naseer •

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?

Thread Thread
 
weilunshrimp profile image
weilun-shrimp •

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/rand is 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.