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
-
Integrate the JavaScript SDK into your project:
- use
npm
:
npm install pubnub
- or download one of our builds from our CDN:
-
Configure your keys:
pubnub = new PubNub({
publishKey : "myPublishKey",
subscribeKey : "mySubscribeKey",
uuid: "myUniqueUUID"
})
Add event listeners
pubnub.addListener({
message: function (m) {
},
presence: function (p) {
},
signal: function (s) {
},
objects: (objectEvent) => {
},
messageAction: function (ma) {
},
file: function (event) {
},
status: function (s) {
},
});
Publish/subscribe
var publishPayload = {
channel : "hello_world",
message: {
title: "greeting",
description: "This is my first message!"
}
}
pubnub.publish(publishPayload, function(status, response) {
console.log(status, response);
})
pubnub.subscribe({
channels: ["hello_world"]
});
Documentation
Support
If you need help or have a general question, contact support@pubnub.com.