Node.jsNode.jsPhoneGapReact V4WebJavaScriptNodeJS V3 to NodeJS V4 Migration Guide

  • V3 initiates via:
    var PUBNUB = require('pubnub');
    var pubnub = PUBNUB.init({ /* params */ })
  • V4 initiates via:
    var PubNub = require('pubnub');
    var pubnub = new PubNub({ /* params */ })
  • V3 accepts a callback as a parameter to announce when messages come in.
    var pubnub = require("pubnub")({
        ssl           : true,  // <- enable TLS Tunneling over TCP
        publish_key   : "demo",
        subscribe_key : "demo"
    });
     
    /* ---------------------------------------------------------------------------
    Publish Messages
    --------------------------------------------------------------------------- */
    var message = { "Hello" : "World!" };
    pubnub.publish({
        channel   : 'hello_world',
        message   : message,
        callback  : function(e) { 
            console.log( "SUCCESS!", e );
        },
        error     : function(e) { 
            console.log( "FAILED! RETRY PUBLISH!", e );
        }
    });
     
    /* ---------------------------------------------------------------------------
    Listen for Messages
    --------------------------------------------------------------------------- */
    pubnub.subscribe({
        channel  : "hello_world",
        message : function(message) {
            console.log( " > ", message );
        }
    });
  • V4 switches to the listener pattern for messages, presence and status. Please refer to the listener documentation to get started http://www.pubnub.com/docs/nodejs-javascript/api-reference-publish-and-subscribe#listeners
    function publish() {
      
        pubnub = new PubNub({
            publishKey : 'demo',
            subscribeKey : 'demo'
        })
          
        function publishSampleMessage() {
            console.log("Since we're publishing on subscribe connectEvent, we're sure we'll receive the following publish.");
            var publishConfig = {
                channel : "hello_world",
                message : "Hello from PubNub Docs!"
            }
            pubnub.publish(publishConfig, function(status, response) {
                console.log(status, response);
            })
        }
          
        pubnub.addListener({
            status: function(statusEvent) {
                if (statusEvent.category === "PNConnectedCategory") {
                    publishSampleMessage();
                }
            },
            message: function(message) {
                console.log("New Message!!", message);
            },
            presence: function(presenceEvent) {
                // handle presence
            }
        })      
        console.log("Subscribing..");
        pubnub.subscribe({
            channels: ['hello_world'] 
        });
    };
    
    
  • V3 contains callback as part of the first argument:
    pubnub.history({ 
        channel: 'hi', 
        callback: function(){}, 
        error: function(){}
    })
  • V4 splits the convention with two params: arguments and callback
    pubnub.history(
    	{ 
    		channel: 'hi'
    	},
    	function (status, response) {
    		console.log(status, response);
    	}
    )
  • V3 uses snake_case for argument names.
  • V4 uses camelCase for argument names. Please refer to the documentation for the functions used for the new naming conventions.
  • V3 calls here_now when a subscribe connection is established.
  • V4 does not call hereNow, please call it manually as needed (from status on PNConnectedCategory or from presence on join for the current user).
  • V3 exposes the connect callback on subscribe.
  • V4 exposes the functionality via the listener on status portion of the listener, please listen to PNConnectedCategory on the status category.
  • V4 returns a status object for each transactional request in the callback and in the status portion of the listeners for non-transactional requests.
  • In V4 passing logVerbosity: true during initialization makes the SDK output logging information useful for debugging.