Subscribing to a Channel
Once a Universal Messaging channel (nChannel) has been found, you can subscribe to the channel, and receive Universal Messaging Events published on the channel.
Simple Subscription
This Flex code snippet demonstrates how to subscribe to a channel:
var startEventID : Long = Long.ZERO;
channel.addSubscriber(nEventListener, startEID,
subscriptionCompleteCallbackFunc);
Note that the addSubscriberFromEID call is asynchronous; it returns immediately, allowing single-threaded Flex clients to continue processing.
To let the developer know when the subscription request has actually completed, the addSubscriber function takes three parameters:
1. nEventListener : the implemented listener that will be called whenever a Universal Messaging Event is received on the channel. The event (nConsumeEvent) object will be passed to this callback function as a parameter.
2. startEID : the ID of the event from which the subscription should start. We use 0 in this example to ensure we receive all available events.
3. postSubCB : the name of a developer-defined Flex function that will be called immediately after the subscription request actually completes.
Subscription with a Filtering Selector
It is also possible to subscribe to a channel with a user-specified selector (a type of filter), ensuring that your client receives only events that match the selector. Selectors are SQL-like statements such as:
name LIKE '%bank%' AND description IS NOT NULL
(vol > 0.5 OR price = 0) AND delta < 1
This Flex code snippet demonstrates how to subscribe to a channel and receive only events which have a key named "volatility" and a value greater than 0.5:
var startEventID : int = 0;
var selector : String = "volatility > 0.5"
channel.addSubscriber(nEventListener, startEID,
subscriptionCompleteCallbackFunc,selector);
Handling Received Events
As discussed above, you must implement a nEventListener to handle any received events. Below is an example of an implemented function from the listener.
public function go(event:nConsumeEvent){
var dictionary:nEventProperties = event.properties;
var sender:String = dictionary.get("sender").toString();
var message:String = dictionary.get("message").toString()
}