Product Updates

What’s New in JavaScript SDK V.4

2 min readPubNub Staff on Aug 9, 2016
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

I am pretty excited to let you know that we just revamped our JavaScript SDK to version 4! The new SDK has been rewritten from the ground up to improve it with better performance, smaller footprint, e...

I am pretty excited to let you know that we just revamped our JavaScript SDK to version 4!

The new SDK has been rewritten from the ground up to improve it with better performance, smaller footprint, event emitter patterns, and provide you easier-to-use APIs. Changes include:

  • Isomorphic Javascript, works on Node, Web, React and others.
  • New networking components with isomorphic Dependencies.
  • Unified presence, message, status notifiers with all the V4 SDKs (Java/Android, iOS / OS X, Ruby).
  • ES6 native classes or ES5 compiled code for older browsers.
  • Cryptography reworked for lighter footprint.
  • Verbose Logging via a simple config switch.
  • Flowtype interfaces bundled in.
  • New reconnection policies to handle spotty internet connections on mobile clients.

Changes in APIs

In the version 4, not only the source code itself, but the exposed APIs are pretty different from the previous version.

To try it out on web, include the version of SDK:

<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.0.4.min.js"></script>

For Node.js, install from npm:

$ npm install pubnub

Note: If you need to use the Node.js SDK < 4, you need to install the older version (pubnub@3.15.2).

and include it with ES5:

PubNub = require('pubnub');

or with ES6:

import PubNub from 'pubnub';

Initialization

You can just instantiate the PubNub object, instead of using init method as in the previous version:

var pubnub = new PubNub({
 subscribeKey: 'my-subscribe-key',
 publishKey: 'my-publish-key'
});

Listeners

In v4, we use the event emitter pattern, instead of callbacks:

pubnub.addListener({
 message: function(message) {
   // handle incoming messages
 },
 presence: function(presence) {
   // handle incoming presence events.
 },
//...
});

Subscribing

To subscribe, you no longer use the success callbacks as you do with v3.x, but instead, use the event listener to listen to the message event.

Also, channel is now channels (plural) that takes an array.

pubnub.subscribe({
  channels: ['doge-channel'],
  withPresence: true // this also subscribes to presence instances
});

Publishing

The publish function uses the traditional success callback, as seen in v3.x, however, the v4 comes with extra params.

pubnub.publish({
  message: {name: 'tacocat'}, 
  channel: 'cat-only-channel',
  sendByPost: false, // send via POST 
  storeInHistory: false, // override default storage options
  meta: {'so': 'meta'}, // publish extra meta data
  function (status, response) {
    // handle status, response
 }
});

For more details for the new SDK and all the deltas between 3.x and 4, see the JavaScript SDK v4 Documentation.

More from PubNub

Can Empathy Exist in the Metaverse
News May 16, 20221 min read

Can Empathy Exist in the Metaverse

A roundtable discussion led by PubNub’s COO, Casey Clegg, exploring the topics of what it means to be human in a virtual world.

PubNub Staff

PubNub Staff

How to Advance Telehealth and Virtual Care Technologies
News May 2, 20221 min read

How to Advance Telehealth and Virtual Care Technologies

Dr. Joe Kvedar, Chair of the Board for the American Telemedicine Association, joins our COO, Casey Clegg, to discuss why...

PubNub Staff

PubNub Staff

Another Step Towards Data Security: ISO-27001 Implementation
BuildMay 2, 20221 min read

Another Step Towards Data Security: ISO-27001 Implementation

Today, we are glad to announce that we are currently in the process of implementing ISO-27001 security standards.

PubNub Staff

PubNub Staff