PubNub JavaScript SDK (V4)

This is the official PubNub JavaScript SDK repository.
PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms.
Get keys
You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.
Tutorial Video

Watch Getting Started with PubNub JS SDK on Dashcam
Configure PubNub
pubnub = new PubNub({
publishKey: 'myPublishKey',
subscribeKey: 'mySubscribeKey',
userId: 'myUniqueUserId',
});
Add event listeners
const channel = pubnub.channel('my_channel');
const subscription = channel.subscription();
subscription.subscribe();
subscription.onMessage = (messageEvent) => { console.log("Message event: ", messageEvent); };
subscription.onPresence = (presenceEvent) => { console.log("Presence event: ", presenceEvent); };
subscription.onMessage = (messageEvent) => { console.log("Message event: ", messageEvent); };
subscription.onPresence = (presenceEvent) => { console.log("Presence event: ", presenceEvent); };
subscription.onSignal = (signalEvent) => { console.log("Signal event: ", signalEvent); };
subscription.onObjects = (objectsEvent) => { console.log("Objects event: ", objectsEvent); };
subscription.onMessageAction = (messageActionEvent) => { console.log("Message Action event: ", messageActionEvent); };
subscription.onFile = (fileEvent) => { console.log("File event: ", fileEvent); };
subscription.addListener({
message: function (m) {
const channelName = m.channel;
const channelGroup = m.subscription;
const pubTT = m.timetoken;
const msg = m.message;
const publisher = m.publisher;
},
presence: function (p) {
const action = p.action;
const channelName = p.channel;
const occupancy = p.occupancy;
const state = p.state;
const channelGroup = p.subscription;
const publishTime = p.timestamp;
const timetoken = p.timetoken;
const uuid = p.uuid;
},
signal: function (s) {
const channelName = s.channel;
const channelGroup = s.subscription;
const pubTT = s.timetoken;
const msg = s.message;
const publisher = s.publisher;
},
objects: (objectEvent) => {
const channel = objectEvent.channel;
const channelGroup = objectEvent.subscription;
const timetoken = objectEvent.timetoken;
const publisher = objectEvent.publisher;
const event = objectEvent.event;
const type = objectEvent.type;
const data = objectEvent.data;
},
messageAction: function (ma) {
const channelName = ma.channel;
const publisher = ma.publisher;
const event = ma.event;
const type = ma.data.type;
const value = ma.data.value;
const messageTimetoken = ma.data.messageTimetoken;
const actionTimetoken = ma.data.actionTimetoken;
},
file: function (event) {
const channelName = event.channel;
const channelGroup = event.subscription;
const publisher = event.publisher;
const timetoken = event.timetoken;
const message = event.message;
const fileId = event.file.id;
const fileName = event.file.name;
const fileUrl = event.file.url;
}
});
Publish/subscribe
const channel = pubnub.channel('my_channel');
const subscription = channel.subscription();
subscription.subscribe();
try {
const result = await pubnub.publish({
message: {
such: "object",
},
channel: "my_channel",
sendByPost: false,
storeInHistory: false,
meta: {
cool: "meta",
},
});
} catch (status) {
console.log(status);
}
Documentation
Support
If you need help or have a general question, contact support@pubnub.com.