How to use pubnub - 10 common examples

To help you get started, we’ve selected a few pubnub examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github girliemac / RPi-KittyCam / kittyCam.js View on Github external
function deletePhoto(imgPath) {
  fs.unlink(imgPath, (err) => {
    if (err) {
       return console.error(err);
    }
    console.log(imgPath + ' is deleted.');
  });
}


// PubNub to publish the data
// to make a separated web/mobile interface can subscribe the data to stream the photos in realtime.

const channel = 'kittyCam';

const pubnub = require('pubnub').init({
  subscribe_key: config.pubnub.subscribe_key,
  publish_key: config.pubnub.publish_key
});

function publish(url, timestamp) {
  pubnub.publish({
    channel: channel,
    message: {image: url, timestamp: timestamp},
    callback: (m) => {console.log(m);},
    error: (err) => {console.log(err);}
  });
}

// Nexmo to send SMS

const Nexmo = require('nexmo');
github pubnub / workshop-raspberrypi / web / eon / motion.js View on Github external
var raspi = require('raspi-io');
var five = require('johnny-five');
var board = new five.Board({io: new raspi()});
var PUBNUB = require('pubnub');

var pubnub = PUBNUB.init({
  publish_key: 'demo',
  subscribe_key: 'demo'
});

board.on('ready', function() {

  console.log('board is ready');

  // Create a new `motion` hardware instance.
  var pin = new five.Pin({
    pin: 'P1-7'
  }); //pin 7 (GPIO 4)

  setInterval(function(){

    pin.query(function(state) {
github pubnub / pubnub-api / titanium / 3.1 / mobile / app.js View on Github external
(function(){

    // -------------------------------------
    // INIT PUBNUB
    // -------------------------------------
    var pubnub = require('pubnub').init({
        publish_key   : 'demo',
        subscribe_key : 'demo',
        ssl           : false,
        origin        : 'pubsub.pubnub.com'
    });

    // -------------------------------------
    // LISTEN FOR MESSAGES
    // -------------------------------------
    pubnub.subscribe({
        channel  : 'test',
        connect  : function() {
            // You can Receive Messages!
            send_a_message("Hello World! #1");
            send_a_message("Hello World! #2");
            send_a_message("Hello World! #3");
github ShareIt-project / WebP2P.io / dist / WebP2P.js View on Github external
function Connector_PubNub(config_init, config_mess, max_connections)
{
  HandshakeConnector.call(this, max_connections);

  var self = this;

  var channel = config_mess.channel;


  // Connect a handshake connector to the PubNub server
  var pubnub = PUBNUB.init(config_init);

  // Configure handshake connector
  pubnub.subscribe(
  {
    channel: channel,
    restore: false,
    backfill: false,

    connect:    self._open,
    message:    self._message,
    disconnect: self._close,

    error: function(response)
    {
      self._error(new Error(response ? response.error : 'Undefined error'))
    }
github sfrechette / stream-sequelize-node / ingest.js View on Github external
type: sequelize.DOUBLE,
        allowNull: false
    }
}, {
    timestamps: false,
    freezeTableName: true
});

// Initialize PubNub client
var pubnub = new pubnub({
    ssl: true,
    subscribe_key: 'sub-c-4377ab04-f100-11e3-bffd-02ee2ddab7fe'
});

// Subscribe (listen on) to channel 
pubnub.subscribe({
    channels: ['pubnub-market-orders']
});

// Handle message payload
pubnub.addListener({
    message: function (message) {
        console.log(message.message);
        connection.sync({
            //logging: ()=>{} 
        })
        .then(function () {
            // Build and Save message stream to database 
            var orderInstance = order.build({
                order_time: message.message.timestamp,
                trade_type: message.message.trade_type,
                symbol: message.message.symbol,
github sfrechette / stream-sequelize-node / ingest.js View on Github external
freezeTableName: true
});

// Initialize PubNub client
var pubnub = new pubnub({
    ssl: true,
    subscribe_key: 'sub-c-4377ab04-f100-11e3-bffd-02ee2ddab7fe'
});

// Subscribe (listen on) to channel 
pubnub.subscribe({
    channels: ['pubnub-market-orders']
});

// Handle message payload
pubnub.addListener({
    message: function (message) {
        console.log(message.message);
        connection.sync({
            //logging: ()=>{} 
        })
        .then(function () {
            // Build and Save message stream to database 
            var orderInstance = order.build({
                order_time: message.message.timestamp,
                trade_type: message.message.trade_type,
                symbol: message.message.symbol,
                order_quantity: message.message.order_quantity,
                bid_price: message.message.bid_price
            })
            //orderInstance.save()
                //if (Math.random() > .9) {
github PubNubDevelopers / Ninja-Multiplayer-Platformer / js / blockscode.js View on Github external
db.get(keyName).then((value) => {
      // console.log('get', keyName, value);
      if (value === null || value.time < request.message.time || true) {
        value = { time: timetoken, coinCache: request.message.coinCache };
        db.set(keyName, value, 1);
        // console.log('set', keyName, value);
      }
       // console.log("value", value);
       // console.log('set', keyName, value);
      // return request.ok();
      pubnub.publish({
        channel: 'realtimephaserFire2',
        message: { value, fromServer: true }
      }).then((publishResponse) => {
        // console.log(publishResponse);
      });
    });
  });
github PubNubDevelopers / Ninja-Multiplayer-Platformer / js / blockscode.js View on Github external
pubnub.publish({
        channel: 'realtimephaserFire2',
        message: {
          value,
          int: true,
          sendToRightPlayer: request.message.uuid
        }
      }).then((publishResponse) => {
          // console.log(publishResponse);
      });
    });
    return request.ok(); // Return a promise when you're done
  }
    // console.log("spitout", request.message)

  pubnub.time().then((timetoken) => {
   // console.log("time", timetoken);
    db.get(keyName).then((value) => {
      // console.log('get', keyName, value);
      if (value === null || value.time < request.message.time || true) {
        value = { time: timetoken, coinCache: request.message.coinCache };
        db.set(keyName, value, 1);
        // console.log('set', keyName, value);
      }
       // console.log("value", value);
       // console.log('set', keyName, value);
      // return request.ok();
      pubnub.publish({
        channel: 'realtimephaserFire2',
        message: { value, fromServer: true }
      }).then((publishResponse) => {
        // console.log(publishResponse);
github ringcentral / ringcentral-js-widgets / packages / ringcentral-integration / integration-test / mock / pubnub.js View on Github external
constructor({ subscribeKey }) {
    this._subscribeKey = subscribeKey;
    this._realPubnub = new RealPubnub({ subscribeKey });
    this.encrypt = this._realPubnub.encrypt;
    this.decrypt = this._realPubnub.decrypt;
    this._channels = [];
    this._listeners = [];
    pubnubs.push(this);
  }
github ringcentral / ringcentral-js-widgets / packages / ringcentral-integration / integration-test / mock / pubnub.js View on Github external
mockMessage(msg) {
    const pubnubMsg = {
      channel: this._channels[0],
      subscription: undefined,
      actualChannel: null,
      subscribedChannel: this._channels[0],
      timetoken: '14933652238078468',
      publisher: undefined,
      message: msg,
    };
    this._listeners.forEach((l) => l.message(pubnubMsg));
  }
}

MockedPubNub.OPERATIONS = RealPubnub.OPERATIONS;
MockedPubNub.CATEGORIES = RealPubnub.CATEGORIES;

export function getLastPubnub() {
  return pubnubs[pubnubs.length - 1];
}

function mockPubnub() {
  const id = require.resolve('pubnub');
  if (require.cache[id]) {
    require.cache[id].exports = MockedPubNub;
  }
}

mockPubnub();

pubnub

Publish & Subscribe Real-time Messaging with PubNub

SEE LICENSE IN LICENSE
Latest version published 3 days ago

Package Health Score

86 / 100
Full package analysis