Nirvana JavaScript : Publishing Events to a NirvanaChannel

Once the NirvanaSession has been established with the Nirvana realm server, and a Nirvana Channel object (NirvanaChannel) has been created, a new Nirvana Event object (Nirvana.nConsumeEvent) must be constructed prior to use in the publish call being made to the channel.

Note that in this example code, we also create a Nirvana Event Dictionary object (Nirvana.nEventDictionary) for our Nirvana Event before publishing it:

var demoChannel = new NirvanaChannel("/some/demo/channel");

var event = new Nirvana.nConsumeEvent();
var dictionary = new Nirvana.nEventProperties();
dictionary.put("exampleKey", "Hello World");
event.setDictionary(dictionary);

demoChannel.publish(event);

Note that the publish call is asynchronous; it returns immediately, allowing single-threaded JavaScript clients to continue processing.

To enable the developer to know when a publish call has completed, a callback function (demoChannel.onPublish(responseCode)) will be invoked by the API. Developers can override this function with their own implementation if they wish. A responseCode value of "OK" indicates the publish was successful:

demoChannel.onPublish = function(status) { window.status = "Publish Status: " + status; }