python_sonar/python
hushasha 928c7bad65 First 2016-11-09 16:18:58 +08:00
..
examples First 2016-11-09 16:18:58 +08:00
tests First 2016-11-09 16:18:58 +08:00
LICENSE First 2016-11-09 16:18:58 +08:00
README.md First 2016-11-09 16:18:58 +08:00
migration.md First 2016-11-09 16:18:58 +08:00

README.md

Contact support@pubnub.com for all questions

PubNub Real-time Data Network

Standalone Python Client

Init

pubnub = Pubnub(publish_key="demo", subscribe_key="demo", ssl_on=False)

PUBLISH

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

# Synchronous usage
print pubnub.publish(channel=channel, message=message)

# Asynchronous usage

def callback(message):
    print(message)

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

SUBSCRIBE

# Listen for Messages

channel = 'hello_world'

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(channels=channel, callback=callback, error=error,
                 connect=connect, reconnect=reconnect, disconnect=disconnect)

SUBSCRIBE to group

# Listen for Messages

channel_group = 'group1'

def callback(message, channel_group, 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_group(channel_groups=channel_group, callback=callback, error=error,
                 connect=connect, reconnect=reconnect, disconnect=disconnect)

SUBSCRIBE Synchronous ( compatible with pre-3.5 )

Runs in tight loop if callback return True. Loop ends when callback return False

# Listen for Messages

channel = 'hello_world'

def callback(message):
    print(message)
    return True

pubnub.subscribe_sync(channel=channel, callback=callback)

UNSUBSCRIBE

# Listen for Messages

channel = 'hello_world'

pubnub.unsubscribe(channel=channel)

PRESENCE

# Listen for Presence Event Messages

channel = 'hello_world'

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


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



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

PRESENCE channel group

# Listen for Presence Event Messages

channel_group = 'group1'

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


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



pubnub.presence_group(channel_group=channel_group, callback=callback, error=error)

HERE_NOW

# Get info on who is here right now!

channel = 'hello_world'

# Synchronous usage
print pubnub.here_now(channel=channel)


# Asynchronous usage

def callback(message):
    print(message)

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

HISTORY

# Synchronous usage

print pubnub.history(channel=channel, count=2)

# Asynchronous usage


def callback(message):
    print(message)

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

HISTORY (including timetokens)

# Synchronous usage

print pubnub.history(channel, count=2, include_token=True)

# Asynchronous usage


def callback(message):
    print(message)

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

GRANT

authkey = "abcd"

# Synchronous usage
print pubnub.grant(channel=channel, auth_key=authkey, read=True, write=True)

# Asynchronous usage


def callback(message):
    print(message)

pubnub.grant(channel=channel, auth_key=authkey, read=True, write=True, callback=callback, error=callback)

AUDIT

channel = "hello_world"
authkey = "abcd"

# Synchronous usage
print pubnub.audit(channel=channel, auth_key=authkey)

# Asynchronous usage


def callback(message):
    print(message)

pubnub.audit(channel=channel, auth_key=authkey, callback=callback, error=callback)

REVOKE

channel = "hello_world"
authkey = "abcd"

# Synchronous usage
print pubnub.revoke(channel=channel, auth_key=authkey)

# Asynchronous usage


def callback(message):
    print(message)

pubnub.revoke(channel=channel, auth_key=authkey, callback=callback, error=callback)

CHANNEL GROUP METHODS

List Groups

# Synchronous usage

print pubnub.channel_group_list_groups(namespace='aaa')

# Asynchronous usage

def callback(message):
    print(message)

pubnub.channel_group_list_groups(namespace='aaa', callback=callback, error=callback)

List Channels

# Synchronous usage

print pubnub.channel_group_list_channels(channel_group='dev:abcd')

# Asynchronous usage

def callback(message):
    print(message)

pubnub.channel_group_list_channels(channel_group='dev:abcd', callback=callback, error=callback)

Add Channel

# Synchronous usage

print pubnub.channel_group_add_channel(channel_group='dev:abcd', channel="hi")

# Asynchronous usage

def callback(message):
    print(message)

pubnub.channel_group_add_channel(channel_group='dev:abcd', channel="hi", callback=callback, error=callback)

Remove Channel

# Synchronous usage

print pubnub.channel_group_remove_channel(channel_group='dev:abcd', channel="hi")

# Asynchronous usage

def callback(message):
    print(message)

pubnub.channel_group_remove_channel(channel_group='dev:abcd', channel="hi", callback=callback, error=callback)

List Channels

# Synchronous usage

print pubnub.channel_group_list_channels(channel_group='dev:abcd')

# Asynchronous usage

def callback(message):
    print(message)

pubnub.channel_group_add_channel(channel_group='dev:abcd', callback=callback, error=callback)

Grant

# Synchronous usage

print pubnub.grant(channel_group='dev:abcd', read=True, write=True, manage=True, auth_key="abcd")

# Asynchronous usage

def callback(message):
    print(message)

pubnub.grant(channel_group='dev:abcd', read=True, write=True, manage=True, auth_key="abcd", callback=callback, error=callback)

Revoke

# Synchronous usage

print pubnub.revoke(channel_group='dev:abcd', auth_key="abcd")

# Asynchronous usage

def callback(message):
    print(message)

pubnub.revoke(channel_group='dev:abcd', auth_key="abcd", callback=callback, error=callback)

Audit

# Synchronous usage

print pubnub.audit(channel_group='dev:abcd')

# Asynchronous usage

def callback(message):
    print(message)

pubnub.audit(channel_group='dev:abcd', callback=callback, error=callback)

Contact support@pubnub.com for all questions