Universal Messaging 10.11 | Developer Guide | Web Client APIs | Web Developer's Guide for Javascript | Web Client Development in JavaScript | Message Queue Tasks | Subscribing to a Queue
 
Subscribing to a Queue
Once a Universal Messaging Queue object has been created, you can subscribe to the queue, and receive Universal Messaging Events published on the queue. JavaScript supports two kinds of queue subscribers. An asynchronous non-transactional consumer and a asynchronous transactional consumer.
Simple Subscription
Once a Universal Messaging Queue object has been created, you can subscribe to the channel, and receive Universal Messaging Events published on the queue.
This JavaScript code snippet demonstrates how to subscribe to a queue:

var myQueue = mySession.getQueue("/demo/prices");

function myEventHandler(event) {
var dictionary = event.getDictionary();
console.log(dictionary.get("name") + " " + dictionary.get("bid"));
}

myQueue.on(Nirvana.Observe.DATA, myEventHandler);

myQueue.subscribe();
Note that the subscribe() call is asynchronous; it returns immediately, allowing single-threaded JavaScript clients to continue processing. Whenever an event is received on the queue, however, any user function assigned as a callback for the observable event Nirvana.Observe.DATA will be invoked, with the appropriate Event as its parameter.
Handling Errors
You may optionally specify an error handler to be notified of subscription or publishing errors:

function myErrorHandler(error) {
console.log(error.message);
}

myQueue.on(Nirvana.Observe.ERROR, myErrorHandler);
If you do not implement an error handler in this way, errors will be silently ignored.