Skip to content

pubnub/javascript

Repository files navigation

PubNub JavaScript SDK (V4)

Codacy BadgenpmBowerKnown Vulnerabilities

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

Getting Started with PubNub JS SDK

Watch Getting Started with PubNub JS SDK on Dashcam

Configure PubNub

  1. Integrate the JavaScript SDK into your project:

  2. Configure your keys:

pubnub=newPubNub({publishKey: 'myPublishKey',subscribeKey: 'mySubscribeKey',userId: 'myUniqueUserId',});

Add event listeners

// create a subscription from a channel entityconstchannel=pubnub.channel('my_channel');constsubscription=channel.subscription();subscription.subscribe();// Event-specific listenerssubscription.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);};// Generic listenerssubscription.addListener({// Messagesmessage: function(m){constchannelName=m.channel;// Channel on which the message was publishedconstchannelGroup=m.subscription;// Channel group or wildcard subscription match (if exists)constpubTT=m.timetoken;// Publish timetokenconstmsg=m.message;// Message payloadconstpublisher=m.publisher;// Message publisher},// Presence// requires a subscription with presencepresence: function(p){constaction=p.action;// Can be join, leave, state-change, or timeoutconstchannelName=p.channel;// Channel to which the message belongsconstoccupancy=p.occupancy;// Number of users subscribed to the channelconststate=p.state;// User stateconstchannelGroup=p.subscription;// Channel group or wildcard subscription match, if anyconstpublishTime=p.timestamp;// Publish timetokenconsttimetoken=p.timetoken;// Current timetokenconstuuid=p.uuid;// UUIDs of users who are subscribed to the channel},// Signalssignal: function(s){constchannelName=s.channel;// Channel to which the signal belongsconstchannelGroup=s.subscription;// Channel group or wildcard subscription match, if anyconstpubTT=s.timetoken;// Publish timetokenconstmsg=s.message;// Payloadconstpublisher=s.publisher;// Message publisher},// App Contextobjects: (objectEvent)=>{constchannel=objectEvent.channel;// Channel to which the event belongsconstchannelGroup=objectEvent.subscription;// Channel groupconsttimetoken=objectEvent.timetoken;// Event timetokenconstpublisher=objectEvent.publisher;// UUID that made the callconstevent=objectEvent.event;// Name of the event that occurredconsttype=objectEvent.type;// Type of the event that occurredconstdata=objectEvent.data;// Data from the event that occurred},// Message ReactionsmessageAction: function(ma){constchannelName=ma.channel;// Channel to which the message belongsconstpublisher=ma.publisher;// Message publisherconstevent=ma.event;// Message action added or removedconsttype=ma.data.type;// Message action typeconstvalue=ma.data.value;// Message action valueconstmessageTimetoken=ma.data.messageTimetoken;// Timetoken of the original messageconstactionTimetoken=ma.data.actionTimetoken;// Timetoken of the message action},// File Sharingfile: function(event){constchannelName=event.channel;// Channel to which the file belongsconstchannelGroup=event.subscription;// Channel group or wildcard subscription match (if exists)constpublisher=event.publisher;// File publisherconsttimetoken=event.timetoken;// Event timetokenconstmessage=event.message;// Optional message attached to the fileconstfileId=event.file.id;// File unique idconstfileName=event.file.name;// File nameconstfileUrl=event.file.url;// File direct URL}});

Publish/subscribe

constchannel=pubnub.channel('my_channel');constsubscription=channel.subscription();subscription.subscribe();try{constresult=awaitpubnub.publish({message: {such: "object",},channel: "my_channel",sendByPost: false,// true to send via poststoreInHistory: false,//override default Message Persistence optionsmeta: {cool: "meta",},// publish extra meta with the request});}catch(status){console.log(status);}

Documentation

Support

If you need help or have a general question, contact [email protected].

About

PubNub JavaScript SDK docs https://www.pubnub.com/docs/sdks/javascript

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 41