Nirvana JavaScript : Using a NirvanaChannel
The Nirvana Channel object (NirvanaChannel) is a simple JavaScript wrapper for a native Nirvana Channel.
Note that unlike the Enterprise APIs, the JavaScript API does not support programmatic creation of channels; instead, you can use the Enterprise Manager GUI to create a Nirvana Channel.
This JavaScript code snippet demonstrates how to create a JavaScript NirvanaChannel object, which allows you to publish or subscribe to such a channel:
var demoChannel = new NirvanaChannel("/some/demo/channel");
NirvanaChannel offers three important functions:
- demoChannel.subscribe()
- demoChannel.unsubscribe()
- demoChannel.publish(Nirvana.nConsumeEvent event)
Each of these functions will invoke a minimal callback function which is implemented in the NirvanaChannel object, but which you can (and probably should) override with your own business-specific implementations.
The callback functions are:
-
NirvanaChannel.onData(nEvent)
invoked when an event is received from a channel
-
NirvanaChannel.onSubscribe(channelName, msg)
invoked after successfully subscribing (an empty implementation can be fine)
-
NirvanaChannel.onUnsubscribe(channelName)
invoked after successfully unsubscribing (an empty implementation can be fine)
-
NirvanaChannel.onPublish(status)
invoked after an event is successfully published (an empty implementation can be fine)
-
NirvanaChannel.onError(errorMessage)
invoked after any unexpected error
As mentioned above, each of these callback functions can be overriden in your code either with an anonymous function:
demoChannel.onData = function(event) {
// do something with the event
}
or, if you prefer, with a named function within your code:
function myEventHandler(event) {
// do something with the event
}
demoChannel.onData = myEventHandler;
See Subscribing to a channel and
Publishing Events to a channel.
