diff options
| author | Devendra | 2014-12-27 14:02:23 +0530 | 
|---|---|---|
| committer | Devendra | 2014-12-27 14:02:23 +0530 | 
| commit | 1064afc424f122164ea4041477f36c326189e135 (patch) | |
| tree | 0c1f034787f9c4fa0323ce20d222ebe82234cff2 | |
| parent | c4d860f05941b7b9ca837c6576b2b70bbda35c04 (diff) | |
| download | pubnub-python-1064afc424f122164ea4041477f36c326189e135.tar.bz2 | |
adding channel group unsubscribe, presence
| -rw-r--r-- | Pubnub.py | 90 | ||||
| -rw-r--r-- | python-tornado/examples/presence_group.py | 65 | ||||
| -rw-r--r-- | python-tornado/examples/subscribe_group.py | 67 | ||||
| -rw-r--r-- | python/examples/subscribe_group.py | 29 | 
4 files changed, 230 insertions, 21 deletions
| @@ -643,6 +643,33 @@ class PubnubBase(object):          else:              return None +    def leave_channel(self, channel, callback=None, error=None): +        ## Send leave +        return self._request({"urlcomponents": [ +            'v2', 'presence', +            'sub_key', +            self.subscribe_key, +            'channel', +            channel, +            'leave' +        ], 'urlparams': {'auth': self.auth_key, 'pnsdk' : self.pnsdk, "uuid": self.uuid,}}, +            callback=self._return_wrapped_callback(callback), +            error=self._return_wrapped_callback(error)) + +    def leave_group(self, channel_group, callback=None, error=None): +        ## Send leave +        return self._request({"urlcomponents": [ +            'v2', 'presence', +            'sub_key', +            self.subscribe_key, +            'channel', +            ',', +            'leave' +        ], 'urlparams': {'auth': self.auth_key, 'pnsdk' : self.pnsdk, 'channel-group' : channel_group, "uuid": self.uuid,}}, +            callback=self._return_wrapped_callback(callback), +            error=self._return_wrapped_callback(error)) + +      def publish(self, channel, message, callback=None, error=None):          """Publishes data on a channel. @@ -718,6 +745,25 @@ class PubnubBase(object):          """          return self.subscribe(channel+'-pnpres', callback=callback) +    def presence_group(self, channel_group, callback, error=None): +        """Subscribe to presence data on a channel group. +            +           Only works in async mode + +        Args: +            channel_group: Channel  group name ( string ) on which to publish message +            callback: A callback method should be passed to the method. +                      If set, the api works in async mode.  +                      Required argument when working with twisted or tornado . +            error: Optional variable. An error method can be passed to the method. +                      If set, the api works in async mode.  +                      Required argument when working with twisted or tornado . + +        Returns: +            None +        """ +        return self.subscribe_group(channel_group+'-pnpres', callback=callback) +      def here_now(self, channel, callback=None, error=None):          """Get here now data. @@ -1164,9 +1210,14 @@ class PubnubCoreAsync(PubnubBase):              self.subscribe_sync(args)              return -        def _invoke(func, msg=None, channel=None): +        def _invoke(func, msg=None, channel=None, real_channel=None):              if func is not None: -                if msg is not None and channel is not None: +                if msg is not None and channel is not None and real_channel is not None: +                    try: +                        func(get_data_for_user(msg), channel, real_channel) +                    except: +                        func(get_data_for_user(msg), channel) +                elif msg is not None and channel is not None:                      func(get_data_for_user(msg), channel)                  elif msg is not None:                      func(get_data_for_user(msg)) @@ -1340,7 +1391,7 @@ class PubnubCoreAsync(PubnubBase):                                      chobj = self.subscriptions[ch[1]]                                  _invoke(chobj['callback'],                                          self.decrypt(response_list[ch[0]]), -                                        channel_list_2[ch[0]])                     +                                        chobj['name'], channel_list_2[ch[0]])                                          elif len(response) > 2:                          channel_list = response[2].split(',')                          response_list = response[0] @@ -1404,21 +1455,12 @@ class PubnubCoreAsync(PubnubBase):          self._connect()      def unsubscribe(self, channel): -        """Subscribe to presence data on a channel. +        """Unsubscribe from channel .             Only works in async mode          Args:              channel: Channel name ( string ) on which to publish message -            message: Message to be published ( String / int / double / dict / list ). -            callback: A callback method should be passed to the method. -                      If set, the api works in async mode.  -                      Required argument when working with twisted or tornado . -            error: Optional variable. An error method can be passed to the method. -                      If set, the api works in async mode.  -                      Required argument when working with twisted or tornado . -        Returns: -            Returns a list in sync mode i.e. when callback argument is not given          """          if channel in self.subscriptions is False:              return False @@ -1430,6 +1472,28 @@ class PubnubCoreAsync(PubnubBase):                  self.subscriptions[channel]['subscribed'] = False                  self.subscriptions[channel]['timetoken'] = 0                  self.subscriptions[channel]['first'] = False +                self.leave_channel(channel=channel) +        self.CONNECT() + +    def unsubscribe_group(self, channel_group): +        """Unsubscribe from channel group. +           Only works in async mode + +        Args: +            channel_group: Channel group name ( string ) on which to publish message + +        """ +        if channel_group in self.subscription_groups is False: +            return False + +        ## DISCONNECT +        with self._channel_group_list_lock: +            if channel_group in self.subscription_groups: +                self.subscription_groups[channel_group]['connected'] = 0 +                self.subscription_groups[channel_group]['subscribed'] = False +                self.subscription_groups[channel_group]['timetoken'] = 0 +                self.subscription_groups[channel_group]['first'] = False +                self.leave_group(channel_group=channel_group)          self.CONNECT() diff --git a/python-tornado/examples/presence_group.py b/python-tornado/examples/presence_group.py new file mode 100644 index 0000000..bb89420 --- /dev/null +++ b/python-tornado/examples/presence_group.py @@ -0,0 +1,65 @@ +## www.pubnub.com - PubNub Real-time push service in the cloud. +# coding=utf8 + +## PubNub Real-time Push APIs and Notifications Framework +## Copyright (c) 2010 Stephen Blum +## http://www.pubnub.com/ + + +import sys +from Pubnub import PubnubTornado as Pubnub + +publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' +subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' +secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo' +cipher_key = len(sys.argv) > 4 and sys.argv[4] or 'abcd' +ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False + +## ----------------------------------------------------------------------- +## Initiate Pubnub State +## ----------------------------------------------------------------------- +pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key, +                secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on) + +channel = 'ab' + + +# Asynchronous usage + +def callback_abc(message, channel, real_channel): +    print(str(message) + ' , ' + channel + ', ' + real_channel) +    #pubnub.unsubscribe_group(channel_group='abc') +    #pubnub.stop() + + +def callback_d(message, channel): +    print(str(message) + ' , ' + channel) + + + +def error(message): +    print("ERROR : " + str(message)) + + +def connect_abc(message): +    print("CONNECTED " + str(message)) + +def connect_d(message): +    print("CONNECTED " + str(message)) +    pubnub.unsubscribe(channel='d') + + +def reconnect(message): +    print("RECONNECTED " + str(message)) + + +def disconnect(message): +    print("DISCONNECTED " + str(message)) + +print pubnub.channel_group_add_channel(channel_group='abc', channel="bn") + +pubnub.presence_group(channel_group='abc', callback=callback_abc, error=error) + +pubnub.presence(channel='d', callback=callback_d, error=error) + +pubnub.start() diff --git a/python-tornado/examples/subscribe_group.py b/python-tornado/examples/subscribe_group.py new file mode 100644 index 0000000..eddcf8d --- /dev/null +++ b/python-tornado/examples/subscribe_group.py @@ -0,0 +1,67 @@ +## www.pubnub.com - PubNub Real-time push service in the cloud. +# coding=utf8 + +## PubNub Real-time Push APIs and Notifications Framework +## Copyright (c) 2010 Stephen Blum +## http://www.pubnub.com/ + + +import sys +from Pubnub import PubnubTornado as Pubnub + +publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' +subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' +secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo' +cipher_key = len(sys.argv) > 4 and sys.argv[4] or 'abcd' +ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False + +## ----------------------------------------------------------------------- +## Initiate Pubnub State +## ----------------------------------------------------------------------- +pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key, +                secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on) + +channel = 'ab' + + +# Asynchronous usage + +def callback_abc(message, channel, real_channel): +    print(str(message) + ' , ' + channel + ', ' + real_channel) +    pubnub.unsubscribe_group(channel_group='abc') +    pubnub.stop() + + +def callback_d(message, channel): +    print(str(message) + ' , ' + channel) + + + +def error(message): +    print("ERROR : " + str(message)) + + +def connect_abc(message): +    print("CONNECTED " + str(message)) + +def connect_d(message): +    print("CONNECTED " + str(message)) +    pubnub.unsubscribe(channel='d') + + +def reconnect(message): +    print("RECONNECTED " + str(message)) + + +def disconnect(message): +    print("DISCONNECTED " + str(message)) + +print pubnub.channel_group_add_channel(channel_group='abc', channel="b") + +pubnub.subscribe_group(channel_groups='abc', callback=callback_abc, error=error, +                 connect=connect_abc, reconnect=reconnect, disconnect=disconnect) + +pubnub.subscribe(channels='d', callback=callback_d, error=error, +                 connect=connect_d, reconnect=reconnect, disconnect=disconnect) + +pubnub.start() diff --git a/python/examples/subscribe_group.py b/python/examples/subscribe_group.py index 3cebcd9..ee8e190 100644 --- a/python/examples/subscribe_group.py +++ b/python/examples/subscribe_group.py @@ -7,7 +7,7 @@  import sys -from Pubnub import Pubnub +from Pubnub import Pubnub as Pubnub  publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'  subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' @@ -19,13 +19,20 @@ ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False  ## Initiate Pubnub State  ## -----------------------------------------------------------------------  pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key, -                secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on, daemon=False) +                secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on)  channel = 'ab'  # Asynchronous usage -def callback(message, channel): + +def callback_abc(message, channel, real_channel): +    print(str(message) + ' , ' + channel + ', ' + real_channel) +    pubnub.unsubscribe_group(channel_group='abc') +    #pubnub.stop() + + +def callback_d(message, channel):      print(str(message) + ' , ' + channel) @@ -34,9 +41,13 @@ def error(message):      print("ERROR : " + str(message)) -def connect(message): +def connect_abc(message):      print("CONNECTED " + str(message)) +def connect_d(message): +    print("CONNECTED " + str(message)) +    pubnub.unsubscribe(channel='d') +  def reconnect(message):      print("RECONNECTED " + str(message)) @@ -47,8 +58,10 @@ def disconnect(message):  print pubnub.channel_group_add_channel(channel_group='abc', channel="b") -pubnub.subscribe_group(channel_groups='abc', callback=callback, error=callback, -                 connect=connect, reconnect=reconnect, disconnect=disconnect) +pubnub.subscribe_group(channel_groups='abc', callback=callback_abc, error=error, +                 connect=connect_abc, reconnect=reconnect, disconnect=disconnect) + +pubnub.subscribe(channels='d', callback=callback_d, error=error, +                 connect=connect_d, reconnect=reconnect, disconnect=disconnect) -pubnub.subscribe(channels='d', callback=callback, error=callback, -                 connect=connect, reconnect=reconnect, disconnect=disconnect) +pubnub.start() | 
