Nirvana C# .NET: Subscribe Asynchronously to a Channel

Asynchronous channel subscribers consume events from a callback on an interface that all asynchronous subscribers must implement. We call this interface an nEventListener.

The listener interface defines one method called 'go' which when called will pass events to the consumer as they are delivered from the Nirvana Realm Server.

A simple example of such a listener is shown below:

public class mySubscriber : nEventListener {

	public mySubscriber() {
		// construct your session and channel objects here
		// begin consuming events from the beginning of the channel (event id 0)
		myChannel.addSubscriber(this, 0);
	}

	public void go(nConsumeEvent event) {
		Console.WriteLine("Consumed event " + event.getEventID());
	}

	public static void Main(String[] args) {
		new mySubscriber();
	}

}

Subscription with a Filtering Selector

Asynchronous consumers can also be created using a selector, which allows the subscription to be filtered based on event properties and their values.

For example, assume some events are being published with the following event properties:

nEventProperteis props = new nEventProperties();
props.put("BONDNAME", "bond1");

A developer can create a message selector string such as:

String selector = "BONDNAME='bond1'";

Passing this string into the addSubscriber method shown in the example code, will ensure that the subscriber will only consume messages that contain the correct value for the event property BONDNAME.

Examples

The following example gives full application source code to asynchronously subscribe to a channel:

For more information on Nirvana publish / subscribe, please see the API documentation.

Share this page with others:
Tell Your Tweets Facebook It! Add to Delicious Reddit! Digg It! Stumble Upon Add to Your Faves Mixx it
Follow Us:
Keep up with my-Channels on Twitter Become a fan on Facebook LinkedIn Profile Recent Highlights RSS Feed