PubNub

PubNub

IoT

API to make realtime applications with variety of hardware devices including Arduino, RPi, Kinoma, Atmel, Samsung Smart TV WebOS, and more. PubNub Platform provides the out-of-the-box infrastructure, network, SDKs, and tools you need to build scalable realtime applications, leaving you free to focus on solving business problems. PubNub guarantees 99.999% reliability, and offers sub-100ms latency anywhere in the world.

Visit API🔁 Alternatives

📚 Documentation & Examples

Everything you need to integrate with PubNub

🚀 Quick Start Examples

PubNub Javascript Examplejavascript
// PubNub API Example
const response = await fetch('https://www.pubnub.com/docs', {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json'
    }
});

const data = await response.json();
console.log(data);

Exploring the PubNub API Documentation

PubNub is a globally-distributed Data Stream Network (DSN) that powers low-latency messaging, geolocation, and IoT devices. They offer a wide range of APIs that developers can use to build real-time applications for various industries. In this blog, we will explore the PubNub API documentation and see how to use them with JavaScript.

Getting Started

To get started with PubNub, you need to sign up for an account first. The free account allows you to make up to 1,000,000 transactions per month, which is perfect for small to medium-sized applications. After you have signed up, you can log in to your dashboard and create a new app.

API Basics

PubNub offers a wide range of APIs, including messaging, presence, storage, and playback. These APIs can be accessed using HTTP requests with the following basic syntax:

http://pubsub.pubnub.com/{version}/{publish_key}/{subscribe_key}/{operation}/{channel}/{optional_arguments}

Here, {version} refers to the API version, {publish_key} and {subscribe_key} are your unique API keys, {operation} refers to the API endpoint, {channel} is the channel that you want to subscribe to or publish to, and {optional_arguments} can be any additional parameters that you want to pass.

Publishing

Here's an example of how to publish a message using the PubNub publish() API in JavaScript:

const pubnub = new PubNub({
    publishKey: 'YOUR_PUBLISH_KEY',
    subscribeKey: 'YOUR_SUBSCRIBE_KEY'
});

const message = {text: 'Hello, world!'};

pubnub.publish({
    channel: 'my_channel',
    message: message,
}, function(status, response) {
    if (status.error) {
        console.log(status.error);
    } else {
        console.log("message Published w/ timetoken", response.timetoken);
    }
});

Subscribing

Here's an example of how to subscribe to messages using PubNub's subscribe() API in JavaScript:

const pubnub = new PubNub({
    subscribeKey: 'YOUR_SUBSCRIBE_KEY'
})

pubnub.addListener({
    message: function(m) {
        console.log('Message:', m);
    },
    presence: function(p) {
        console.log('Presence:', p);
    }
});

pubnub.subscribe({
    channels: ['my_channel']
});

Presence

You can also track presence events, such as the number of users subscribed to a channel, using the hereNow() API:

pubnub.hereNow({
    channels: ['my_channel'],
    includeUUIDs: true,
    includeState: true
}, function(status, response) {
    if (status.error) {
        console.log(status.error);
    } else {
        console.log("hereNow Response: ", response);
    }
});

Storage

PubNub also offers a key-value storage API that you can use to store and retrieve data. Here's an example of how to use the set() and get() APIs to store and retrieve data:

const pubnub = new PubNub({
    publishKey: 'YOUR_PUBLISH_KEY',
    subscribeKey: 'YOUR_SUBSCRIBE_KEY'
});

const keyName = 'my_key';
const value = 'hello, world!';

pubnub.set({
    channel: 'my_channel',
    key: keyName,
    value: value
}, function(status, response) {
    if (status.error) {
        console.log(status.error);
    } else {
        console.log("set Response: ", response);
    }
});

pubnub.get({
    channel: 'my_channel',
    key: keyName
}, function(status, response) {
    if (status.error) {
        console.log(status.error);
    } else {
        console.log("get Response: ", response);
    }
});

Conclusion

In this blog, we explored the PubNub API documentation and saw how to use various APIs with JavaScript. PubNub offers a reliable and scalable solution for real-time messaging and data streaming, making it an ideal choice for developers building real-time applications.

📊 30-Day Uptime History

Daily uptime tracking showing online vs offline minutes

Sep 12Sep 14Sep 16Sep 18Sep 20Sep 22Sep 24Sep 26Sep 28Sep 30Oct 2Oct 4Oct 6Oct 8Oct 1104008001440Minutes
Online
Offline

Related APIs in IoT