Functiondojox.cometd.subscribe

dojo.require("dojox.cometd._base");
defined in dojox/cometd/_base.js

dojox.cometd.subscribe() handles all the hard work of telling the server that we want to be notified when events are published on a particular topic. subscribe accepts a function to handle messages and returns a dojo.Deferred object which has an extra property added to it which makes it suitable for passing to dojox.cometd.unsubscribe() as a "subscription handle" (much like the handle object that dojo.connect() produces and which dojo.disconnect() expects).

Note that of a subscription is registered before a connection with the server is established, events sent before the connection is established will not be delivered to this client. The deferred object which subscribe returns will callback when the server successfuly acknolwedges receipt of our "subscribe" request.

Usage

function (/*String*/ channel, /*Object*/ objOrFunc, /*String*/ funcName, /*Object?*/ props) (view source)
parametertypedescription
channelString 
objOrFuncObjectan object scope for funcName or the name or reference to a function to be called when messages are delivered to the
funcNameStringthe second half of the objOrFunc/funcName pair for identifying a callback function to notifiy upon channel message delivery
propsObjectOptional.

Examples

Example 1

Simple subscribe use-case

dojox.cometd.init("http://myserver.com:8080/cometd");
// log out all incoming messages on /foo/bar
dojox.cometd.subscribe("/foo/bar", console, "debug");

Example 2

Subscribe before connection is initialized

dojox.cometd.subscribe("/foo/bar", console, "debug");
dojox.cometd.init("http://myserver.com:8080/cometd");

Example 3

Subscribe an unsubscribe

dojox.cometd.init("http://myserver.com:8080/cometd");
var h = dojox.cometd.subscribe("/foo/bar", console, "debug");
dojox.cometd.unsubscribe(h);

Example 4

Listen for successful subscription:

dojox.cometd.init("http://myserver.com:8080/cometd");
var h = dojox.cometd.subscribe("/foo/bar", console, "debug");
h.addCallback(function(){
    console.debug("subscription to /foo/bar established");
});