Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
PubNub is a real-time communication platform that allows developers to build applications with real-time messaging, presence, and data synchronization capabilities. It is commonly used for chat applications, live updates, IoT device communication, and more.
Real-time Messaging
This feature allows you to send real-time messages to a specific channel. The code sample demonstrates how to publish a message to a channel using PubNub.
const PubNub = require('pubnub');
const pubnub = new PubNub({
publishKey: 'your-publish-key',
subscribeKey: 'your-subscribe-key'
});
pubnub.publish({
channel: 'my_channel',
message: { text: 'Hello, World!' }
}, (status, response) => {
if (status.error) {
console.log('Publish failed: ', status);
} else {
console.log('Message published with timetoken', response.timetoken);
}
});
Real-time Subscription
This feature allows you to subscribe to a channel and receive real-time messages. The code sample demonstrates how to listen for new messages on a subscribed channel.
const PubNub = require('pubnub');
const pubnub = new PubNub({
publishKey: 'your-publish-key',
subscribeKey: 'your-subscribe-key'
});
pubnub.addListener({
message: function(event) {
console.log('New message received: ', event.message);
}
});
pubnub.subscribe({
channels: ['my_channel']
});
Presence Detection
This feature allows you to detect the presence of users in a channel. The code sample demonstrates how to listen for presence events such as join, leave, and timeout.
const PubNub = require('pubnub');
const pubnub = new PubNub({
publishKey: 'your-publish-key',
subscribeKey: 'your-subscribe-key'
});
pubnub.addListener({
presence: function(event) {
console.log('Presence event: ', event);
}
});
pubnub.subscribe({
channels: ['my_channel'],
withPresence: true
});
History
This feature allows you to retrieve the message history of a channel. The code sample demonstrates how to fetch the last 10 messages from a channel's history.
const PubNub = require('pubnub');
const pubnub = new PubNub({
publishKey: 'your-publish-key',
subscribeKey: 'your-subscribe-key'
});
pubnub.history({
channel: 'my_channel',
count: 10
}, (status, response) => {
if (status.error) {
console.log('History retrieval failed: ', status);
} else {
console.log('Message history: ', response.messages);
}
});
Socket.IO is a library that enables real-time, bidirectional, and event-based communication between web clients and servers. It is commonly used for chat applications, live updates, and real-time analytics. Compared to PubNub, Socket.IO is more focused on web sockets and may require more server-side setup.
Pusher is a hosted service that makes it easy to add real-time data and functionality to web and mobile applications. It offers features like real-time messaging, presence, and webhooks. Pusher is similar to PubNub in terms of functionality but is a hosted service with a different pricing model.
Ably is a real-time messaging service that provides pub/sub messaging, presence, and data synchronization. It is designed for high scalability and reliability. Ably offers similar features to PubNub but emphasizes performance and global data distribution.
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.
You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.
Watch Getting Started with PubNub JS SDK on Dashcam
Integrate the JavaScript SDK into your project:
npm
:
npm install pubnub
Configure your keys:
pubnub = new PubNub({
publishKey: 'myPublishKey',
subscribeKey: 'mySubscribeKey',
userId: 'myUniqueUserId',
});
// create a subscription from a channel entity
const channel = pubnub.channel('my_channel');
const subscription = channel.subscription();
subscription.subscribe();
// Event-specific listeners
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); };
// Generic listeners
subscription.addListener({
// Messages
message: function (m) {
const channelName = m.channel; // Channel on which the message was published
const channelGroup = m.subscription; // Channel group or wildcard subscription match (if exists)
const pubTT = m.timetoken; // Publish timetoken
const msg = m.message; // Message payload
const publisher = m.publisher; // Message publisher
},
// Presence
// requires a subscription with presence
presence: function (p) {
const action = p.action; // Can be join, leave, state-change, or timeout
const channelName = p.channel; // Channel to which the message belongs
const occupancy = p.occupancy; // Number of users subscribed to the channel
const state = p.state; // User state
const channelGroup = p.subscription; // Channel group or wildcard subscription match, if any
const publishTime = p.timestamp; // Publish timetoken
const timetoken = p.timetoken; // Current timetoken
const uuid = p.uuid; // UUIDs of users who are subscribed to the channel
},
// Signals
signal: function (s) {
const channelName = s.channel; // Channel to which the signal belongs
const channelGroup = s.subscription; // Channel group or wildcard subscription match, if any
const pubTT = s.timetoken; // Publish timetoken
const msg = s.message; // Payload
const publisher = s.publisher; // Message publisher
},
// App Context
objects: (objectEvent) => {
const channel = objectEvent.channel; // Channel to which the event belongs
const channelGroup = objectEvent.subscription; // Channel group
const timetoken = objectEvent.timetoken; // Event timetoken
const publisher = objectEvent.publisher; // UUID that made the call
const event = objectEvent.event; // Name of the event that occurred
const type = objectEvent.type; // Type of the event that occurred
const data = objectEvent.data; // Data from the event that occurred
},
// Message Reactions
messageAction: function (ma) {
const channelName = ma.channel; // Channel to which the message belongs
const publisher = ma.publisher; // Message publisher
const event = ma.event; // Message action added or removed
const type = ma.data.type; // Message action type
const value = ma.data.value; // Message action value
const messageTimetoken = ma.data.messageTimetoken; // Timetoken of the original message
const actionTimetoken = ma.data.actionTimetoken; // Timetoken of the message action
},
// File Sharing
file: function (event) {
const channelName = event.channel; // Channel to which the file belongs
const channelGroup = event.subscription; // Channel group or wildcard subscription match (if exists)
const publisher = event.publisher; // File publisher
const timetoken = event.timetoken; // Event timetoken
const message = event.message; // Optional message attached to the file
const fileId = event.file.id; // File unique id
const fileName = event.file.name;// File name
const fileUrl = event.file.url; // File direct URL
}
});
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, // true to send via post
storeInHistory: false, //override default Message Persistence options
meta: {
cool: "meta",
}, // publish extra meta with the request
});
} catch (status) {
console.log(status);
}
If you need help or have a general question, contact support@pubnub.com.
v8.2.10
October 31 2024
Actions
type definition.Payload
type.Payload
type definition with objects which can be serialized by JSON.stringify
using toJSON()
methods. Fixed the following issues reported by @yo1dog: #412.FAQs
Publish & Subscribe Real-time Messaging with PubNub
The npm package pubnub receives a total of 155,627 weekly downloads. As such, pubnub popularity was classified as popular.
We found that pubnub demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.