Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pubnub

Package Overview
Dependencies
Maintainers
7
Versions
229
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pubnub

Publish & Subscribe Real-time Messaging with PubNub

  • 8.3.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
7
Created

What is pubnub?

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.

What are pubnub's main functionalities?

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);
  }
});

Other packages similar to pubnub

Keywords

FAQs

Package last updated on 12 Dec 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc