WebSocket and HTTP/1.1 clients for Nimony, using sequential .passive I/O.
WebSockets deliver complete text or binary messages with automatic fragmentation
and ping/pong handling. HTTP supports streaming response bodies and keep-alive.
Both clients support verified TLS and absolute deadlines.
Requires Linux, Nimony 0.6.3 and a C compiler.
Add a relative path to tsuru/src to your project's nimony.paths, or compile
with --path:/path/to/tsuru/src.
Import tsuru/websocket or tsuru/httpclient; these are the only public
modules. Both export the shared Header, HttpResponse, header and
Deadline (afterMs, never), so they can be imported together.
Compile with -d:tsuruTls for wss:// and https://. TLS requires OpenSSL
headers and libraries: openssl-devel on Fedora or libssl-dev on Debian/Ubuntu.
import std/[opt, syncio]
import tsuru/websocket
proc chat() {.passive, raises.} =
let ws = connectWebSocket("ws://127.0.0.1:8080/")
defer: ws.abort()
if not ws.send("Hello from Nimony"): return
case ws.recv()
of Some(message): echo message.data
of None(): echo "Receive deadline expired"
discard ws.close()Start the task with submit(delay chat()) on std/threadpool.
Every caller of a suspending operation must also be passive.
The echo example includes scheduler startup and error handling:
nimony c -o:build/echo examples/echo.nim
build/echo ws://localhost:8080/chatReceive expiry returns None and keeps the connection usable. Configure limits,
authentication and subprotocols through initWebSocketOptions.
See the WebSocket guide and receive example.
import std/syncio
import tsuru/httpclient
proc fetch() {.passive, raises.} =
let deadline = afterMs(10_000)
let c = connectHttp("https://example.com/data", deadline)
defer: c.close()
let response = c.request(deadline)
echo response.status
echo c.readAll()One request deadline covers sending, response headers and body reads.
Use readBody with caller-owned storage for large responses. Finish the body
before the next request, or close immediately.
The HTTP example includes scheduler startup and error handling:
nimony c -d:tsuruTls -o:build/http_get examples/http_get.nim
build/http_get https://example.com/See the HTTP guide for headers, framing, limits and errors.
| Guide | Contents |
|---|---|
| Runtime | Passive tasks, connection ownership, deadlines, TLS and platform support |
| WebSocket | Options, messages, receive expiry, closing and API contracts |
| HTTP | Requests, response streaming, headers, keep-alive and errors |
| Verification | Test commands, configurations and fixture coverage |
tests/run --network
tests/run --tls --asan --networkTests use local fixtures. See verification for requirements and optimized builds.
MIT licensed.