Introduction
nanomsg is a socket library written in C that provides several common communication patterns for building distributed systems. Created by Martin Sustrik (co-creator of ZeroMQ), it simplifies inter-process and network messaging with a POSIX-like API and built-in scalability protocols.
What nanomsg Does
- Implements messaging patterns: PAIR, PIPELINE, REQ/REP, PUB/SUB, SURVEY, and BUS
- Works over TCP, IPC (Unix sockets), in-process, and WebSocket transports
- Provides automatic reconnection and load balancing across endpoints
- Handles message framing, routing, and delivery semantics per protocol
- Exposes a simple BSD socket-like C API (nn_socket, nn_send, nn_recv)
Architecture Overview
nanomsg uses a layered architecture with transports at the bottom (TCP, IPC, inproc, WebSocket) and scalability protocols on top. Each protocol defines its own routing and delivery semantics. The library runs an internal I/O thread pool that manages connections and message queuing asynchronously. The public API is synchronous by default with non-blocking options available via nn_poll or socket options.
Self-Hosting & Configuration
- Build from source with CMake or install via system package managers
- Link against libnanomsg in your C/C++ project with
-lnanomsg - Set socket options for buffer sizes, timeouts, and reconnect intervals via nn_setsockopt
- Bind a socket to listen on an address, or connect to a remote endpoint
- Language bindings exist for Python, Go, Rust, Java, Ruby, Node.js, and more
Key Features
- Seven built-in scalability protocols covering most messaging patterns
- Transport-agnostic: switch between TCP, IPC, and inproc by changing the address string
- Automatic reconnection on connection loss with configurable retry intervals
- Zero-copy message API for high-throughput scenarios
- Clean C API with no external dependencies beyond the C standard library
Comparison with Similar Tools
- ZeroMQ — more mature ecosystem with more patterns and language bindings; nanomsg offers a cleaner API and pluggable transports
- nng — the spiritual successor to nanomsg by the same author, with an async-first API
- gRPC — RPC framework with code generation; nanomsg provides raw messaging patterns
- NATS — server-based pub/sub; nanomsg is a brokerless peer-to-peer library
FAQ
Q: What is the difference between nanomsg and nng? A: nng (nanomsg-next-gen) is the successor project with a redesigned async API and better scalability. nanomsg remains stable and widely deployed.
Q: Does nanomsg require a message broker? A: No. nanomsg is fully brokerless. Peers connect directly to each other.
Q: Which transport should I use for local IPC?
A: Use ipc:// for inter-process on the same host or inproc:// for threads within the same process.
Q: What license does nanomsg use? A: MIT license.