1. Tutorial¶
This tutorial introduces Seth-Pusher core concepts by example.
1.1. Getting started¶
Before starting installation, creation of virtualenv is advised. To install server run:
$ pip install -U sethpusher
1.2. Basic usage¶
$ seth-pusher --debug=true
SethPusher relies on sockjs so developing js client make sure to take a look at:
When debug flag is set to true we can inspect basic statistics by:
$ curl http://localhost:8080/debug/
or
$ curl curl http://localhost:8080/debug/channels/<channel_name>/
1.3. Using redis pubsub functionality¶
It requires redis-server and additional setup so first:
$ sudo apt-get install redis-server
$ sudo service redis-server start
Install additional requirements:
$ pip install -U redis
$ pip install -U hiredis (optionally)
$ pip install -U tornado-redis==2.4.18
Run:
$ seth-pusher --debug=true --app='sethpusher.applications.SethPusherOnRedisApplication'
1.4. Specify host & port¶
$ seth-pusher --debug=true --host=127.0.0.1 --port=8003
1.5. Connecting via websocket or other transport¶
By default predefined applications expose transport connection via /connect endpoint.
1.6. Connecting, subscribing & unsubscring¶
Those messages are handled by /connect endpoint by default.
Connecting:
{ 'type': 'connect', 'subscriber_id': '<unique_user_id>' }
Subscribing:
{ 'type': 'subscribe', 'channel': '<channel_name>' }
Unsubscribing:
{ 'type': 'unsubscribe', 'channel': '<channel_name>' }
1.7. Publishing to channels¶
In order to subscribe/unsubsribe from channel send proper message to ws handler.
$ curl -X POST -H "Content-Type: application/json" -d '{"my": "data"}' http://localhost:8080/api/channels/<channel_name>/
1.8. Sending message to user¶
By default every user that connects to sethpusher is subscribed to all channel which allows us to send him a private message.
$ curl -X POST -H "Content-Type: application/json" -d '{"my": "data"}' http://localhost:8080/api/users/<subscriber_id>/
or
$ curl -X POST -H "Content-Type: application/json" -d '{"my": "data", "sender_id": "<author_id>"}' http://localhost:8080/api/users/<subscriber_id>/
1.9. Creating your own app¶
Do You want your own application class ? Use our AppRunner and mixins / components.
- Create file my_app.py
- Run:
$ python my_app.py --logging=debug
- Test it with:
$ curl http://localhost:6666/debug/\?batman\=bruce_wayne
Code:
from tornado.options import parse_command_line
from sethpusher.default_server import AppRunner
from sethpusher.applications import SethPusherInMemoryApplication
from sethpusher.authenticators import ApiKeyAuthenticationPolicy
from sethpusher.persistors import ExternalApiPersistencePolicy
class MyPersistence(ExternalApiPersistencePolicy):
api_url = 'http://www.my_super_api.com'
def persist(self, data, **kwargs):
print data
print kwargs
print "KABOOM !"
class MyAuth(ApiKeyAuthenticationPolicy):
key_name = u'batman'
key_secret_value = u'bruce_wayne'
class MyVeryOwnApp(SethPusherInMemoryApplication):
def get_authentication_policy(self, **kwargs):
return MyAuth()
def get_persistence_policy(self, **kwargs):
return MyPersistence()
if __name__ == '__main__':
parse_command_line()
runner = AppRunner(port=6666, debug=True)
runner.start(app=MyVeryOwnApp)