ZMQ

ZeroMQ is a high-performance asynchronous messaging library. It can be used to hook up to a node and listen to events. This allows us to listen to a variety of events such as: all transactions coming into the node, all confirmed transactions, etc. The full list of events can be found here.

In order to use it we first need to install Tangle.Net.Zmq NuGet. Furthermore, if we want to hook up onto a node, the node must have ZMQ enabled . Here is an simple example of how to listen to all incoming transactions:

// Subscribe to transactions event
ZmqIriListener.Transactions += (sender, eventArgs) =>
{
    Console.WriteLine("-----------------------");
    Console.WriteLine(eventArgs.Transaction.Hash);
    Console.WriteLine(eventArgs.Transaction.Address);
};

// Start listening to the event type (use MessageType.All) to subscribe to all events
var tokenSource = ZmqIriListener.Listen("tcp://node_url", MessageType.All);

// Listen for 60 seconds
var stopwatch = new Stopwatch();
stopwatch.Start();
while (stopwatch.ElapsedMilliseconds < 600000)
{
    Thread.Sleep(100);
}
// Cancel the thread
tokenSource.Cancel();

We have to be careful because listening to everything is very expensive. That is why we have the MessageType enum. The MessageType is the name of the event as IRI knows it. This means that once we listen to a certain MessageType we will only have the corresponding C# event occurring.

ZMQ is a very useful tool as it allows us to monitor the Tangle in real time. We can monitor transactions, specific addresses and a lot more.

Last updated