This project is read-only.

Say you have messages of some .NET type MyMessage that you want to publish from one application to any interested subscribers. If your type is serialisable using the DataContract serialiser, then you can use the PubSubByType Hydra facility specially designed for this purpose. This provides Publisher<T> and Subscriber<T> classes used as follows.

First you need to create an instance of the Hydra service in both the publishing and subscribing apps. This identifies the set of servers, failover policy between servers, and polling interval. The easiest way to do this is by supplying a list of CouchDb server names and the database used by Hydra ("hydra" below) with this call:

var servers = new List<string> {"server1", "server2"};
_hydraService = new HydraService(new RoundRobinConfigProvider(servers, "hydra"));
(The RoundRobinConfigProvider will use the first server in its list unless it fails, when it will switch to the second, and so on). In your publishing app you then create an instance of the Publisher class and send messages as follows:
var publisher = new Publisher<MyMessage>(_hydraService);
var message = new MyMessage {<set field values>};

In your subscribing app you ask it to listen for incoming messages of type MyMessage by creating a Subscriber<MyMessage> object and handling the MessageOnQueue event:

_subscriber = new Subscriber<MyMessage>(_hydraService);
_subscriber.MessageOnQueue += SubscriberOnMessageOnQueue;
private void SubscriberOnMessageOnQueue(object sender, MyMessage message)
     // Code to handle incoming MyMessage objects 

And that’s it. Each MyMessage object sent by the publisher will raise an event on each subscriber. You can also have as many publishers as you like.

This is the very simplest use of Hydra, where publish/subscribe is based entirely on .NET data types and messages are broadcast to all the world. More complex usages allow messages to be directed to particular destinations, listeners to pick up only messages from particular sources, bidirectional conversations and so on.

Last edited May 9, 2012 at 5:40 PM by NickNorth, version 5


No comments yet.