Coder Social home page Coder Social logo

python's Introduction

PubNub Python SDK

Build Status PyPI PyPI Docs

This is the official PubNub Python SDK repository.

Note: Python SDK version 5.0 no longer supports Python 2.7, Twisted or Tornado, if you still require support for these please use SDK version 4.8.1

PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms.

Get keys

You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.

Configure PubNub

  1. Integrate the Python SDK into your project using pip:

    pip install pubnub
  2. Configure your keys:

    pnconfig = PNConfiguration()
    
    pnconfig.subscribe_key = 'mySubscribeKey'
    pnconfig.publish_key = 'myPublishKey'
    pnconfig.uuid = 'myUniqueUUID'
    pubnub = PubNub(pnconfig)

Add event listeners

class SubscribeHandler(SubscribeCallback):
  def status(self, pubnub, event):
    print("Is there an error? ", event.is_error())
    print("Status value for category: %s" % event.category)
    print("Status value for error_data: %s" % event.error_data)
    print("Status value for error: %s" % event.error)
    print("Status value for status_code: %s" % event.status_code)
    print("Status value for operation: %s" % event.operation)
    print("Status value for tls_enabled: %s" % event.tls_enabled)
    print("Status value for uuid: %s" % event.uuid)
    print("Status value for auth_key: %s" % event.auth_key)
    print("Status value for origin: %s" % event.origin)
    print("Status value for client_request: %s" % event.client_request)
    print("Status value for client_response: %s" % event.client_response)
    print("Status value for original_response: %s" % event.original_response)
    print("Status value for affected_channels: %s" % event.affected_channels)
    print("Status value for affected_groups: %s" % event.affected_groups)

  def presence(self, pubnub, presence):
      pass  # Handle incoming presence data

  def message(self, pubnub, message):
      pass  # Handle incoming messages

  def signal(self, pubnub, signal):
      pass # Handle incoming signals

pubnub.add_listener(SubscribeHandler())

Publish/subscribe

def my_publish_callback(envelope, status):
  if status.is_error():
    ... #handle error here
  else:
    ... #handle result here

pubnub.publish().channel('my_channel').message('Hello world!').pn_async(my_publish_callback)

pubnub.subscribe().channels('my_channel').execute()

Documentation

Support

If you need help or have a general question, contact [email protected].

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.