Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
203 lines (128 loc) · 2.79 KB

File metadata and controls

203 lines (128 loc) · 2.79 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

PubNub 3.5.0 Web Data Push Cloud-hosted API - PYTHON

www.pubnub.com - PubNub Web Data Push Service in the Cloud.

Import

# Pre 3.5:
from Pubnub import Pubnub

# New in 3.5+
from Pubnub import PubnubTornado as Pubnub

Init


# Pre 3.5:
pubnub = Pubnub(
    "demo",  ## PUBLISH_KEY
    "demo",  ## SUBSCRIBE_KEY
     False   ## SSL_ON?
)

# New in 3.5+
pubnub = Pubnub(publish_key="demo", subscribe_key="demo", ssl_on=False)

PUBLISH

channel = 'hello_world'
message = 'Hello World !!!'

# Pre 3.5:
def callback(messages):
    print(messages)

pubnub.publish( {
    'channel'  : channel,
    'message'    : message,
    'callback' : callback
})

# New in 3.5+

def callback(message):
    print(message)

pubnub.publish(channel, message, callback=callback, error=callback)

SUBSCRIBE


# Listen for Messages

channel = 'hello_world'

# Pre 3.5:
def connected() :
    print('CONNECTED')

def message_received(message):
    print(message)

pubnub.subscribe({
    'channel'  : channel,
    'connect'  : connected,
    'callback' : message_received
})

# New in 3.5+

def callback(message, channel):
    print(message)


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


def connect(message):
    print("CONNECTED")


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


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


pubnub.subscribe(channel, callback=callback, error=callback,
                 connect=connect, reconnect=reconnect, disconnect=disconnect)

Unsubscribe

Once subscribed, you can easily, gracefully, unsubscribe:

# Pre 3.5:
pubnub.unsubscribe({
    'channel' : 'hello_world'
})

# New in 3.5+

pubnub.unsubscribe(channel='hello_world')

PRESENCE


# Pre 3.5:
#

# New in 3.5+

# Listen for Presence Event Messages

channel = 'hello_world'

def callback(message, channel):
    print(message)


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

pubnub.presence(channel, callback=callback, error=callback)

HERE_NOW


channel = 'hello_world'

# Pre 3.5:
def callback(messages):
    print(messages)

pubnub.here_now( {
    'channel'  : channel,
    'callback' : callback
})


# New in 3.5+

# Get info on who is here right now!


def callback(message):
    print(message)

pubnub.here_now(channel, callback=callback, error=callback)

HISTORY

channel = 'hello_world'

# Pre 3.5:
def history_complete(messages):
    print(messages)

pubnub.history( {
    'channel'  : channel,
    'limit'    : 2,
    'callback' : history_complete
})


# New in 3.5+

def callback(message):
    print(message)

pubnub.history(channel, count=2, callback=callback, error=callback)

IO Event Loop


# Pre 3.5:
tornado.ioloop.IOLoop.instance().start()

# New in 3.5+
pubnub.start()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.