This add-on is operated by PubNub
Visit Pubnub.com
PubNub
Last updated July 28, 2023
This article is a work in progress, or documents a feature that is not yet released to all users. This article is unlisted. Only those with the link can access it.
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
Hands-on Developer Training Webinars
Demo Apps & Sample Data Streams
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
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
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
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
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
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)