NetZeroMQ 0.2.2
Suggested Alternatives
dotnet add package NetZeroMQ --version 0.2.2
NuGet\Install-Package NetZeroMQ -Version 0.2.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetZeroMQ" Version="0.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NetZeroMQ" Version="0.2.2" />
<PackageReference Include="NetZeroMQ" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NetZeroMQ --version 0.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NetZeroMQ, 0.2.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NetZeroMQ@0.2.2
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NetZeroMQ&version=0.2.2
#tool nuget:?package=NetZeroMQ&version=0.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
NetZeroMQ
A modern .NET 8+ binding for ZeroMQ (libzmq) with cppzmq-style API.
Features
- Modern .NET: Built for .NET 8.0+ with LibraryImport source generators
- cppzmq Style: Familiar API for developers coming from C++
- Type Safe: Strongly-typed socket options, message properties, and enums
- Cross-Platform: Supports Windows, Linux, and macOS (x64, ARM64)
- Safe by Default: SafeHandle-based resource management
Installation
dotnet add package NetZeroMQ
Quick Start
REQ-REP Pattern
using NetZeroMQ;
// Server
using var ctx = new Context();
using var server = new Socket(ctx, SocketType.Rep);
server.Bind("tcp://*:5555");
var request = server.RecvString();
server.Send("World");
// Client
using var client = new Socket(ctx, SocketType.Req);
client.Connect("tcp://localhost:5555");
client.Send("Hello");
var reply = client.RecvString();
PUB-SUB Pattern
using NetZeroMQ;
// Publisher
using var ctx = new Context();
using var pub = new Socket(ctx, SocketType.Pub);
pub.Bind("tcp://*:5556");
pub.Send("topic1 Hello subscribers!");
// Subscriber
using var sub = new Socket(ctx, SocketType.Sub);
sub.Connect("tcp://localhost:5556");
sub.Subscribe("topic1");
var message = sub.RecvString();
Router-to-Router Pattern
using System.Text;
using NetZeroMQ;
using var ctx = new Context();
using var peerA = new Socket(ctx, SocketType.Router);
using var peerB = new Socket(ctx, SocketType.Router);
// Set explicit identities for Router-to-Router
peerA.SetOption(SocketOption.Routing_Id, Encoding.UTF8.GetBytes("PEER_A"));
peerB.SetOption(SocketOption.Routing_Id, Encoding.UTF8.GetBytes("PEER_B"));
peerA.Bind("tcp://127.0.0.1:5555");
peerB.Connect("tcp://127.0.0.1:5555");
// Peer B sends to Peer A (first frame = target identity)
peerB.Send(Encoding.UTF8.GetBytes("PEER_A"), SendFlags.SendMore);
peerB.Send("Hello from Peer B!");
// Peer A receives (first frame = sender identity)
var senderId = Encoding.UTF8.GetString(peerA.RecvBytes());
var message = peerA.RecvString();
// Peer A replies using sender's identity
peerA.Send(Encoding.UTF8.GetBytes(senderId), SendFlags.SendMore);
peerA.Send("Hello back from Peer A!");
Polling
using NetZeroMQ;
var items = new PollItem[]
{
new(socket1, PollEvents.In),
new(socket2, PollEvents.In)
};
if (Poller.Poll(items, timeout: 1000) > 0)
{
if (items[0].IsReadable) { /* handle socket1 */ }
if (items[1].IsReadable) { /* handle socket2 */ }
}
Message API
using NetZeroMQ;
// Create and send message
using var msg = new Message("Hello World");
socket.Send(ref msg, SendFlags.None);
// Receive message
using var reply = new Message();
socket.Recv(ref reply, RecvFlags.None);
Console.WriteLine(reply.ToString());
Socket Types
| Type | Description |
|---|---|
SocketType.Req |
Request socket (client) |
SocketType.Rep |
Reply socket (server) |
SocketType.Pub |
Publish socket |
SocketType.Sub |
Subscribe socket |
SocketType.Push |
Push socket (pipeline) |
SocketType.Pull |
Pull socket (pipeline) |
SocketType.Dealer |
Async request |
SocketType.Router |
Async reply |
SocketType.Pair |
Exclusive pair |
API Reference
Context
var ctx = new Context(); // Default
var ctx = new Context(ioThreads: 2, maxSockets: 1024); // Custom
ctx.SetOption(ContextOption.IoThreads, 4);
var threads = ctx.GetOption(ContextOption.IoThreads);
var (major, minor, patch) = Context.Version; // Get ZMQ version
bool hasCurve = Context.Has("curve"); // Check capability
Socket
var socket = new Socket(ctx, SocketType.Req);
// Connection
socket.Bind("tcp://*:5555");
socket.Connect("tcp://localhost:5555");
socket.Unbind("tcp://*:5555");
socket.Disconnect("tcp://localhost:5555");
// Send
socket.Send("Hello");
socket.Send(byteArray);
socket.Send(ref message, SendFlags.SendMore);
bool sent = socket.TrySend(data, out int bytesSent);
// Receive
string str = socket.RecvString();
byte[] data = socket.Recv(buffer);
socket.Recv(ref message);
bool received = socket.TryRecvString(out string? result);
// Options
socket.SetOption(SocketOption.Linger, 0);
int linger = socket.GetOption<int>(SocketOption.Linger);
Requirements
- .NET 8.0 or later
- Native libzmq library (automatically provided via NetZeroMQ.Native package)
License
MIT License - see LICENSE for details.
Related Projects
- libzmq - ZeroMQ core library
- cppzmq - C++ binding (API inspiration)
- libzmq-native - Native binaries
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Microsoft.Extensions.ObjectPool (>= 8.0.0)
- NetZeroMQ.Core (>= 0.2.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.