Skip Navigation
Show nav
Heroku Dev Center
  • Get Started
  • Documentation
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
  • Documentation
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
View categories

Categories

  • Heroku Architecture
    • Dynos (app containers)
    • Stacks (operating system images)
    • Networking & DNS
    • Platform Policies
    • Platform Principles
  • Command Line
  • Deployment
    • Deploying with Git
    • Deploying with Docker
    • Deployment Integrations
  • Continuous Delivery
    • Continuous Integration
  • Language Support
    • Node.js
    • Ruby
      • Working with Bundler
      • Rails Support
    • Python
      • Background Jobs in Python
      • Working with Django
    • Java
      • Working with Maven
      • Java Database Operations
      • Working with Spring Boot
      • Java Advanced Topics
    • PHP
    • Go
      • Go Dependency Management
    • Scala
    • Clojure
  • Databases & Data Management
    • Heroku Postgres
      • Postgres Basics
      • Postgres Getting Started
      • Postgres Performance
      • Postgres Data Transfer & Preservation
      • Postgres Availability
      • Postgres Special Topics
    • Heroku Data For Redis
    • Apache Kafka on Heroku
    • Other Data Stores
  • Monitoring & Metrics
    • Logging
  • App Performance
  • Add-ons
    • All Add-ons
  • Collaboration
  • Security
    • App Security
    • Identities & Authentication
    • Compliance
  • Heroku Enterprise
    • Private Spaces
      • Infrastructure Networking
    • Enterprise Accounts
    • Enterprise Teams
    • Heroku Connect (Salesforce sync)
      • Heroku Connect Administration
      • Heroku Connect Reference
      • Heroku Connect Troubleshooting
    • Single Sign-on (SSO)
  • Patterns & Best Practices
  • Extending Heroku
    • Platform API
    • App Webhooks
    • Heroku Labs
    • Building Add-ons
      • Add-on Development Tasks
      • Add-on APIs
      • Add-on Guidelines & Requirements
    • Building CLI Plugins
    • Developing Buildpacks
    • Dev Center
  • Accounts & Billing
  • Troubleshooting & Support
  • Integrating with Salesforce
  • Add-ons
  • All Add-ons
  • PubNub
PubNub

This add-on is operated by PubNub

Visit Pubnub.com

PubNub

Last updated August 27, 2020

Table of Contents

  • Provisioning the add-on
  • PubNub Developer Resources
  • Installing the PubNub Ruby gem
  • Using PubNub Ruby SDK
  • Ruby SDK Documentation
  • Installing the Java SDK
  • Using the Java SDK
  • Java SDK Documentation
  • Installing the NodeJS SDK
  • Using the NodeJS SDK
  • NodeJS SDK Documentation
  • Installing the Python SDK
  • Using the Python SDK
  • Python SDK Documentation
  • Using PubNub PHP SDK
  • PHP SDK Documentation
  • Support
  • Additional PubNub SDKs
  • Removing the add-on

PubNub is an add-on that lets you connect, scale, and manage realtime applications and IoT devices without having to manage realtime infrastructure.

Use PubNub to send/receive messages in real time across multiple platforms and devices with two simple API calls (Publish/Subscribe - Send/Receive).

Provisioning the add-on

PubNub can be attached to a Heroku application via the CLI:

$ heroku addons:create pubnub

PubNub Developer Resources

PubNub Training Videos

Hands-on Developer Training Webinars

Demo Apps & Sample Data Streams

Tech Blogs

Installing the PubNub Ruby gem

Ruby Gems links to the registered Ruby gem.

$ gem install pubnub

Using PubNub Ruby SDK

require 'pubnub'

publish_key   = ENV['PUBNUB_PUBLISH_KEY'] || 'demo'
subscribe_key = ENV['PUBNUB_SUBSCRIBE_KEY'] || 'demo'

## -----------------------------------------
## Create PubNub Client API (INITIALIZATION)
## -----------------------------------------
puts('Creating new PubNub Client API')

pubnub = Pubnub.new(
     :subscribe_key    => 'demo',
     :publish_key      => 'demo',
     :error_callback   => lambda { |msg|
        puts "ERROR: #{msg.inspect}"
     },
     :connect_callback => lambda { |msg|
        puts "CONNECTED: #{msg.inspect}"
     }
)

## ----------------------
## Send Message (PUBLISH)
## ----------------------
puts('Broadcasting Message')
message = { 'some_data' => 'my data here' }

info = pubnub.publish(
    :channel => "hello_world",
    :message => message
) { |envelope|
   puts("\nchannel: #{envelope.channel}
        \nmsg: #{envelope.message}")
}

## Publish Success?
puts(info)

## --------------------------------
## Request Past Publishes (HISTORY)
## --------------------------------
puts('Requesting History')
messages = pubnub.history(
    :channel => 'hello_world',
    :count   => 100
){ |envelope|
   puts("\nchannel: #{envelope.channel}:
   \nmsg: #{envelope.message}")
}

puts(messages)

Ruby SDK Documentation

  • Overview
  • Tutorials
  • API Reference

Installing the Java SDK

To use Pubnub, simply copy the Pubnub-StandardEdition-3.7.5.jar file into your project’s libs directory. (To use the debug version of Pubnub, simply copy the Pubnub-StandardEdition-Debug-3.7.5.jar file into your project’s libs directory.) You need to also add these dependencies: json-20090211.jar and slf4j-api-1.7.5.jar

Get Code: Using Maven

<dependencies>
..
    <dependency>
        <groupId>com.pubnub</groupId>
        <artifactId>pubnub</artifactId>
        <version>3.7.5</version>
    </dependency>
</dependencies>

Using the Java SDK

Add PubNub to your project then import the code

import com.pubnub.api.*;
import org.json.*;

Now create a Pubnub object and subscribe

Pubnub pubnub = new Pubnub("demo", "demo");

try {
  pubnub.subscribe("my_channel", new Callback() {
      @Override
      public void connectCallback(String channel, Object message) {
          pubnub.publish("my_channel", "Hello from the PubNub Java SDK", new Callback() {});
      }

      @Override
      public void disconnectCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : DISCONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      public void reconnectCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : RECONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      @Override
      public void successCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : " + channel + " : "
                     + message.getClass() + " : " + message.toString());
      }

      @Override
      public void errorCallback(String channel, PubnubError error) {
          System.out.println("SUBSCRIBE : ERROR on channel " + channel
                     + " : " + error.toString());
      }
    }
  );
} catch (PubnubException e) {
  System.out.println(e.toString());
}

Java SDK Documentation

  • Getting Started
  • Tutorials
  • API Reference

Installing the NodeJS SDK

npm install pubnub

Using the NodeJS SDK

var pubnub = require("pubnub")({
    ssl           : true,  // <- enable TLS Tunneling over TCP
    publish_key   : "demo",
    subscribe_key : "demo"
});

/* ---------------------------------------------------------------------------
Publish Messages
--------------------------------------------------------------------------- */
var message = { "Hello" : "World!" };
pubnub.publish({
    channel   : 'hello_world',
    message   : message,
    callback  : function(e) { console.log( "SUCCESS!", e ); },
    error     : function(e) { console.log( "FAILED! RETRY PUBLISH!", e ); }
});

/* ---------------------------------------------------------------------------
Listen for Messages
--------------------------------------------------------------------------- */
pubnub.subscribe({
    channel  : "hello_world",
    callback : function(message) {
        console.log( " > ", message );
    }
});

NodeJS SDK Documentation

  • Getting Started
  • Tutorials
  • API Reference

Installing the Python SDK

pip install pubnub>=3.7.3

Using the Python SDK

Add PubNub to your project using one of the procedures defined above. Include the Python SDK in your code.

from pubnub import PubnubTwisted as Pubnub

Then connect with your keys

pubnub = Pubnub(publish_key="demo", subscribe_key="demo")
def callback(message, channel):
    print(message)

def error(message):
    print("ERROR : " + str(message))

def connect(message):
    print("CONNECTED")
    print pubnub.publish(channel='my_channel', message='Hello from the PubNub Python SDK')

def reconnect(message):
    print("RECONNECTED")

def disconnect(message):
    print("DISCONNECTED")

pubnub.subscribe(channels='my_channel', callback=callback, error=callback,
                 connect=connect, reconnect=reconnect, disconnect=disconnect)

Python SDK Documentation

  • Getting Started
  • Tutorials
  • API Reference

Using PubNub PHP SDK

Developers can use Publish/Subscribe from a terminal or web server like Apache/NginX/lighttpd. PubNub PHP Push API provides real time Publish and Subscribe for PHP Developers.

PHP Examples

# Initialize
$pubnub = new Pubnub(
    "demo",  ## PUBLISH_KEY
    "demo",  ## SUBSCRIBE_KEY
    "",      ## SECRET_KEY
    false    ## SSL_ON?
);

# Publish
$info = $pubnub->publish(array(
    'channel' => 'hello_world', ## REQUIRED Channel to Send
    'message' => 'Hey World!'   ## REQUIRED Message String/Array
));
print_r($info);

# Subscribe
$pubnub->subscribe(array(
    'channel'  => 'hello_world',        ## REQUIRED Channel to Listen
    'callback' => function($message) {  ## REQUIRED Callback With Response
        var_dump($message);  ## Print Message
        return true;         ## Keep listening (return false to stop)
    }
));

# Realtime Join/Leave Presence Events
$pubnub->presence(array(
    'channel'  => $channel,
    'callback' => function($message) {
        print_r($message);
        echo "\r\n";
        return true;
    }
));

# Channel Occupancy
$here_now = $pubnub->here_now(array(
    'channel' => $channel
));

# Message History
$history = $pubnub->detailedHistory(array(
    'channel' => $channel,
    'count'   => 10,
    'end'   => "13466530169226760"
));

PHP SDK Documentation

  • Overview
  • Tutorials
  • API Reference

Support

All PubNub support and runtime issues should be submitted via one of the Heroku Support channels. Any non-support related issues or product feedback is welcome at support@pubnub.com.

Additional PubNub SDKs

www.pubnub.com/developers

Removing the add-on

PubNub can be removed via the CLI.

This will destroy all associated data and cannot be undone!

$ heroku addons:destroy pubnub
-----> Removing pubnub from sharp-mountain-4005... done, v20 (free)

Keep reading

  • All Add-ons

Feedback

Log in to submit feedback.

Ziggeo Pusher Channels

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Podcasts
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing

Subscribe to our monthly newsletter

Your email address:

  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Heroku Podcasts
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Facebook
  • Instagram
  • Github
  • LinkedIn
  • YouTube
Heroku is acompany

 © Salesforce.com

  • heroku.com
  • Terms of Service
  • Privacy
  • Cookies
  • Cookie Preferences