Skip to content

Commit

Permalink
Version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jparreira committed Oct 28, 2015
1 parent 3d95f93 commit 70022ac
Show file tree
Hide file tree
Showing 6 changed files with 1,186 additions and 2 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Realtime Framework

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# RealtimeMessaging-Python3
Realtime Cloud Messaging Python 3.x SDK
## Realtime Cloud Messaging Python 3.x SDK
Part of the [The Realtime® Framework](http://framework.realtime.co), Realtime Cloud Messaging (aka ORTC) is a secure, fast and highly scalable cloud-hosted Pub/Sub real-time message broker for web and mobile apps.

If your application has data that needs to be updated in the user’s interface as it changes (e.g. real-time stock quotes or ever changing social news feed) Realtime Cloud Messaging is the reliable, easy, unbelievably fast, “works everywhere” solution.

## Setup

1. This SDK requires the module websocket.py to be installed. Please process the installation instructions of websocket.py at: [https://github.com/liris/websocket-client](https://github.com/liris/websocket-client)
2. Copy the files `ortc.py` and `ortc_extensibility.py` to your project directory
3. Place in your source code the line: import ortc
4. Follow the sample code in: `example_simple.py` or `example_menu.py`



> NOTE: For simplicity these samples assume you're using a Realtime® Framework developers' application key with the authentication service disabled (every connection will have permission to publish and subscribe to any channel). For security guidelines please refer to the [Security Guide](http://messaging-public.realtime.co/documentation/starting-guide/security.html).
>
> **Don't forget to replace `YOUR_APPLICATION_KEY` and `YOUR_APPLICATION_PRIVATE_KEY` with your own application key. If you don't already own a free Realtime® Framework application key, [get one now](https://app.realtime.co/developers/getlicense).**

## API Reference
[http://messaging-public.realtime.co/documentation/python/2.1.0/index.html](http://messaging-public.realtime.co/documentation/python/2.1.0/index.html)


## Authors
- Realtime
- Eduardo Basterrechea
- Armando Ramos

96 changes: 96 additions & 0 deletions example_menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/python

import ortc

application_key = 'YOUR_APP_KEY'
authentication_token = 'SomeUserToken'
private_key = 'YOUR_PRIVATE_KEY'
cluster_url = "http://ortc-developers.realtime.co/server/2.1"

ortc_client = ortc.OrtcClient()

def on_exception(sender, exception):
print ('ORTC Exception: '+exception)

def on_connected(sender):
print ('ORTC Connected')

def on_disconnected(sender):
print ('ORTC Disconnected')

def on_message(sender, channel, message):
print (r'ORTC Message ('+channel+'): ' + message)

def on_subscribed(sender, channel):
print ('ORTC Subscribed to: '+channel)

def on_unsubscribed(sender, channel):
print ('ORTC Unsubscribed from: '+channel)

def on_reconnecting(sender):
print ('ORTC Reconnecting')

def on_reconnected(sender):
print ('ORTC Reconnected')

def presence_callback(error, result):
if not error == None:
print ('ORTC Presence error: ' + error)
else:
print (result)


ortc_client.set_on_exception_callback(on_exception)
ortc_client.set_on_connected_callback(on_connected)
ortc_client.set_on_disconnected_callback(on_disconnected)
ortc_client.set_on_subscribed_callback(on_subscribed)
ortc_client.set_on_unsubscribed_callback(on_unsubscribed)
ortc_client.set_on_reconnecting_callback(on_reconnecting)
ortc_client.set_on_reconnected_callback(on_reconnected)

ortc_client.cluster_url = cluster_url
ortc_client.connection_metadata = 'pythonExample'

key = ''
while not key == 'q':
print ('\n\nPlease enter your option:\nq-quit, 1-Connect, 2-Disconnect, 3-Subscribe, 4-Unsubscribe, 5-Send message, 6-Save authentication, 7-Enable presence, 8-Disable presence, 9-Presence\n')
key = input('')
if key == '1':
ortc_client.connect(application_key, authentication_token)
if key == '2':
ortc_client.disconnect()
if key == '3':
channel = input('Channel name to subscribe: ')
ortc_client.subscribe(channel, True, on_message)
if key == '4':
channel = input('Channel name to unsubscribe: ')
ortc_client.unsubscribe(channel)
if key == '5':
channel = input('Channel name: ')
message = input('Message: ')
ortc_client.send(channel, message)
if key == '6':
authentication_token = input('Authentication token: ')
is_private = input('Is private? (y/n)')
time_to_live = input('Time to live (sec): ')
ttl = int(time_to_live)
private_key = input('Private key: ')
channels_permissions = {}
add_channel = 'y'
while add_channel == 'y':
channel = input('Channel name: ')
permission = input('Permission (r/w/p): ')
channels_permissions[channel] = permission
add_channel = input('Do you want to add more channels? (y/n): ')
ret = ortc.save_authentication(cluster_url, True, authentication_token, True if is_private=='y' else False, application_key, ttl, private_key, channels_permissions)
print ('Success') if ret==True else print('Failed')
if key == '7':
channel = input('Channel name: ')
ortc.enable_presence(cluster_url, True, application_key, private_key, channel, True, presence_callback)
if key == '8':
channel = input('Channel name: ')
ortc.disable_presence(cluster_url, True, application_key, private_key, channel, presence_callback)
if key == '9':
channel = input('Channel name: ')
ortc.presence(cluster_url, True, application_key, authentication_token, channel, presence_callback)

57 changes: 57 additions & 0 deletions example_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import ortc
import time

application_key = 'YOUR_APP_KEY'

ortc_client = ortc.OrtcClient()

def on_exception(sender, exception):
print ('ORTC Exception: '+exception)

def on_connected(sender):
print ('ORTC Connected')
ortc_client.subscribe('blue', True, on_message)

def on_disconnected(sender):
print ('ORTC Disconnected')
import _thread
_thread.interrupt_main()

def on_message(sender, channel, message):
print (r'ORTC Message ('+channel+'): ' + message)
ortc_client.unsubscribe(channel)

def on_subscribed(sender, channel):
print ('ORTC Subscribed to: '+channel)
ortc_client.send(channel, 'Python API message')

def on_unsubscribed(sender, channel):
print ('ORTC Unsubscribed from: '+channel)
ortc_client.disconnect()

def on_reconnecting(sender):
print ('ORTC Reconnecting')

def on_reconnected(sender):
print ('ORTC Reconnected')

ortc_client.set_on_exception_callback(on_exception)
ortc_client.set_on_connected_callback(on_connected)
ortc_client.set_on_disconnected_callback(on_disconnected)
ortc_client.set_on_subscribed_callback(on_subscribed)
ortc_client.set_on_unsubscribed_callback(on_unsubscribed)
ortc_client.set_on_reconnecting_callback(on_reconnecting)
ortc_client.set_on_reconnected_callback(on_reconnected)

ortc_client.cluster_url = "https://ortc-developers.realtime.co/server/ssl/2.1"

ortc_client.connect(application_key)

try:
while True:
time.sleep(1)
except:
pass
Loading

0 comments on commit 70022ac

Please sign in to comment.