Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Simple Example

DomenicDenicola edited this page · 2 revisions
Clone this wiki locally

server.js

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);
};

client.js

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");
};
Something went wrong with that request. Please try again.