blob: 5f9b350c7c24cdc3aa79848c165178e27bafb85f (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 | ## ---------------------------------------------------
##
## YOU MUST HAVE A PUBNUB ACCOUNT TO USE THE API.
## http://www.pubnub.com/account
##
## ----------------------------------------------------
## ----------------------------------------------------
## PubNub 3.1 Real-time Cloud Push API - PYTHON TWISTED
## ----------------------------------------------------
##
## www.pubnub.com - PubNub Real-time Push Service in the Cloud. 
## http://github.com/pubnub/pubnub-api/tree/master/python-twisted/
##
## PubNub is a Massively Scalable Real-time Service for Web and Mobile Games.
## This is a cloud-based service for broadcasting Real-time messages
## to thousands of web and mobile clients simultaneously.
## ----------------------------------------------------
## Python Twisted Setup
## ----------------------------------------------------
## Download BZ2 archive from http://twistedmatrix.com/
## 
## > tar xvfj Twisted-12.1.0.tar.bz2
## > cd Twisted-12.1.0
## > sudo python setup.py install
## 
## ----------------------------------------------------
## Third Party Libraries Dependency
## ----------------------------------------------------
## You must download and install,
##
## 1. pyopenssl
## Download from https://launchpad.net/pyopenssl
##
## 2. pycrypto
## Download from https://github.com/dlitz/pycrypto OR
## from http://code.google.com/p/uploadprj/downloads/detail?name=pycrypto-2.3.win32-py2.7.zip&can=2&q
## ---------------
## Python Push API
## ---------------
pubnub = Pubnub(
    "demo",  ## PUBLISH_KEY
    "demo",  ## SUBSCRIBE_KEY
    "demo",  ## SECRET_KEY
    "",      ## CIPHER_KEY (Cipher key is Optional)
     False   ## SSL_ON?
)
## -----------------------------------------------------------------------
## IO Event Loop
## -----------------------------------------------------------------------
## VERY IMPORTANT TO ADD THIS LINE AT THE VERY BOTTOM!
##
## reactor.run() ## IMPORTANT!
##
## -----------------------------------------------------------------------
## Subscribe Example
## -----------------------------------------------------------------------
def connected() :
    ## -----------------------------------------------------------------------
    ## Publish Example
    ## -----------------------------------------------------------------------
    def publish_complete(info):
        print(info)
    pubnub.publish({
        'channel' : "my-twisted-channel",
        'message' : {
            'some_text' : 'Hello World!'
        },
        'callback' : publish_complete
    })
def message_received(message):
    print(message)
pubnub.subscribe({
    'channel'  : "my-twisted-channel",
    'connect'  : connected,
    'callback' : message_received
})
## -----------------------------------------------------------------------
## Time Example
## -----------------------------------------------------------------------
def time_complete(timestamp):
    print(timestamp)
pubnub.time({ 'callback' : time_complete })
## -----------------------------------------------------------------------
## History Example
## -----------------------------------------------------------------------
def history_complete(messages):
    print(messages)
pubnub.history( {
    'channel'  : "my-twisted-channel",
    'limit'    : 10,
    'callback' : history_complete
})
## -----------------------------------------------------------------------
## UUID Example
## -----------------------------------------------------------------------
uuid = pubnub.uuid()
print "UUID"
print uuid
## -----------------------------------------------------------------------
## IO Event Loop
## -----------------------------------------------------------------------
reactor.run()
 |