a lightweight WebSocket library for Zig ⚡
- Only allocates for WebSocket handshake, message parsing and building does not allocate
- Ease of use, can be used directly with
net.Stream
- Does buffered reads and writes (can be used with any other reader/writer too)
- Supports streaming output thanks to WebSocket fragmentation
By default, ws uses the Stream
interface of net
namespace.
You can use your choice of stream through ws.Client
interface.
test "Simple connection to :8080" {
const allocator = std.testing.allocator;
var cli = try connect(allocator, try std.Uri.parse("ws://localhost:8080"), &.{
.{"Host", "localhost"},
.{"Origin", "http://localhost/"},
});
defer cli.deinit(allocator);
while (true) {
const msg = try cli.receive();
switch (msg.type) {
.text => {
std.debug.print("received: {s}\n", .{msg.data});
try cli.send(.text, msg.data);
},
.ping => {
std.debug.print("got ping! sending pong...\n", .{});
try cli.pong();
},
.close => {
std.debug.print("close", .{});
break;
},
else => {
std.debug.print("got {s}: {s}\n", .{@tagName(msg.type), msg.data});
},
}
}
try cli.close();
}
- WebSocket server support
- TLS support out of the box (tracks
std.crypto.tls.Client
) - Request & response headers
- WebSocket Compression support
This library wouldn't be possible without these cool projects & posts:
- truemedian/wz
- frmdstryr/zhp
- treeform/ws
- openmymind.net/WebSocket-Framing-Masking-Fragmentation-and-More
- Writing WebSocket servers
MIT License, check out.