简体   繁体   中英

How to unsubscribe from pubnub channel Python SDK

I'm using pubnub with python SDK. I have some testing environment written in python and I'm trying to write python code to add pubnub client subscribe to channel and unsubscribe. In the server I'm checking that the server gets join and leave messages (I'm using presence channel).

The problem is that when the thread dies I don't get leave message and I don't see any unsubscribe call in the python code to do explicit unsubscribe.

Any ideas?

Thanks.

My code looks something like this:

    def main(user_id,channel_name):
        t = Thread(target=rt_subscribe,args=(user_id,channel_name))

    def rt_subscribe(user_id,channel_name):
        def get_message(msg):
            print 'got message: %s' % msg
            return True

        pb = Pubnub(publish_key=PUBLISH_KEY, 
                  subscribe_key=SUBSCRIBE_KEY, 
                  pres_uuid=user_id,
                  ssl_on=False)
        pb.subscribe({
                   'channel'  : channel_name,
                   'callback' : get_message 
                })

PubNub Python SDK Unsubscribe

There are several things to consider here and the first is using the correct SDK for the job as PubNub has 3 Python SDK options available. Also you can invoke a Leave Event forcefully using a simple REST call. But first, if you want to unsubscribe via Python, you may consider using our Python Twisted SDK.

GitHub Repository for PubNub Python Twisted SDK - Download Now

You'll issue subscribe and unsubscribe as follows:

pubnub.subscribe({ 'channel' : chan, 'callback' : receive_processor })
pubnub.unsubscribe({ 'channel' : chan })

Yes. It's that simple. :-)

PubNub Force Leave Event on Presence

You can issue a REST call in the following format to force a leave event on a channel.

https://pubsub.pubnub.com/v2/presence/sub_key/YOUR_SUB_KEY/channel/YOUR_CHANNEL/leave?uuid=YOUR_USER_UUID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM