-
Notifications
You must be signed in to change notification settings - Fork 9
Simple Example
DomenicDenicola edited this page Jan 27, 2012
·
2 revisions
var pubit = require("pubit");
var publish = pubit.makeEmitter(exports, {
onListenerError: function (error) {
console.error(error); // If a client screws up, just log it, don't re-throw.
},
events: ["connect", "disconnect"] // Opt-in to early errors if someone misunderstands our interface.
});
exports.connect = function (user) {
publish("connect", user);
};
exports.disconnect = function (user) {
publish("disconnect", user);
};
var server = require("./server");
server.onNext("connect", function () {
console.log("First user!");
});
server.on("connect", function (user) {
console.log(user + " connected to us");
});
function onDisconnect(user) {
console.log("disconnect");
}
server.on("disconnect", onDisconnect);
exports.turnOffDisconnectNotifications = function () {
server.off("disconnect", onDisconnect);
};
exports.turnOffConnectNotifications = function () {
// Usually you want to unsubscribe by using a function reference, like above, but we're being illustrative.
server.off("connect");
};