see –help or README for more information
Have a look at the README part of this documentation to integrate or extend this library.
This library exposes three modules, two of them based on tornado library. The listener module, ran by the server, waits for incoming connections. When a client, running the client module, connects to the listener, it waits for incoming events from the server. And finally, when the request module is ran, it posts events on the server, who forwards them to the client.
The request module relies only on urllib2.
This module opens a new long polling connection on a listener, and waits for events to come.
platform: | Unix |
---|---|
synopsis: | This module provides an eventsource listener based on tornado |
Class that defines an event, its behaviour and the matching actions
content_type field is the Accept header value that is returned on new connections
ACTIONS contains the list of acceptable POST targets.
Method to create id generation behaviour
Class that defines a JSON-checked Event with id generation
Method to create id generation behaviour
This module listens for incoming connections and forwards events from “request” to “client” using tornado http long-polling on the client side, and http post on the request side.
platform: | Unix |
---|---|
synopsis: | This module provides an eventsource client based on tornado |
This module opens a new connection to an eventsource server, and wait for events.
Function that gets called on non long-polling actions, on error or on end of polling.
Parameters: | response – tornado’s response object that handles connection response data |
---|
This module only connects to listener’s rest POST interface to send new events
Sends a JSON query to eventsource’s URL
Parameters: |
|
---|
It enables a halfduplex communication from server to client, but initiated by the client, through standard HTTP(S) communication.
- Fairly recent python (tested with 2.7)
- Fairly recent tornado (tested with 2.2.1)
Launch the server:
eventsource_listener -P 8888 -i -k 50000
Launch the client:
eventsource_client 42:42:42:42:42:42 -r 5000
Send requests:
eventsource_request 42:42:42:42:42:42 ping “42” eventsource_request 42:42:42:42:42:42 close
eventsource/listener.py or eventsource_server:
- usage: eventsource/listener.py [-h] [-H HOST] [-P PORT] [-d]
[-j] [-k KEEPALIVE] [-i]
Event Source Listener
optional arguments: -h, –help show this help message and exit -H HOST, –host HOST Host to bind on -P PORT, –port PORT Port to bind on -d, –debug enables debug output -j, –json to enable JSON Event -k KEEPALIVE, –keepalive KEEPALIVE
Keepalive timeout
-i, --id to generate identifiers
eventsource/client.py or eventsource_client:
- usage: eventsource/client.py [-h] [-H HOST] [-P PORT] [-d]
[-r RETRY] token
Event Source Client
positional arguments: token Token to be used for connection
optional arguments: -h, –help show this help message and exit -H HOST, –host HOST Host to connect to -P PORT, –port PORT Port to be used connection -d, –debug enables debug output -r RETRY, –retry RETRY
Reconnection timeout
eventsource/send_request.py or eventsource_request:
- usage: eventsource/send_request.py [-h] [-H HOST] [-P PORT] [-j]
token action [data]
Generates event for Event Source Library
positional arguments: token Token to be used for connection action Action to send data Data to be sent
optional arguments: -h, –help show this help message and exit -H HOST, –host HOST Host to connect to -P PORT, –port PORT Port to be used connection -j, –json Treat data as JSON
On the server side, basically all you have to do is to add the following to your code:
from eventsource import listener
- application = tornado.web.Application([
- (r”/(.*)/(.*)”, listener.EventSourceHandler,
- dict(event_class=EVENT,
- keepalive=KEEPALIVE)),
])
application.listen(PORT) tornado.ioloop.IOLoop.instance().start()
PORT is an integer for the port to bind to
KEEPALIVE is an integer for the timeout between two keepalive messages (to protect from disconnections)
To extend the behaviour of the event source library, without breaking eventsource definition, the Event based classes implements all processing elements that shall be done on events.
here is an example to create a new Event that takes multiline data and join it in a one line string seperated with semi-colons.
- class OneLineEvent(Event):
ACTIONS = [“ping”,Event.FINISH]
“”“Property to enable multiline output of the value”“” def get_value(self):
# replace carriage returns by semi-colons # this method shall always return a list (even if one value) return [”;”.join([line for line in self._value.split(‘n’)])]value = property(get_value,set_value)
And now, I want to add basic id support to OneLineEvent, in OneLineIdEvent, nothing is easier :
- class OneLineIdEvent(OneLineEvent,IdEvent):
- id = property(IdEvent.get_value)
Or if I want the id to be a timestamp:
import time class OneLineTimeStampEvent(OneLineEvent):
id = property(lambda s: “%f” % (time.time(),))
To change the way events are generated, you can directly call EventSourceHandler.buffer_event() to create a new event to be sent. But the post action is best, at least while WSGI can’t handle correctly long polling connections.
Python Event Source Library
(c) 2012 Bernard Pratz (c) 2012 (CKAB) hackable:Devices
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
EOF
- http://stackoverflow.com/questions/10665569/websocket-event-source-implementation-to-expose-a-two-way-rpc-to-a-python-dj
- http://stackoverflow.com/questions/8812715/using-a-simple-python-generator-as-a-co-routine-in-a-tornado-async-handler
- http://dev.w3.org/html5/eventsource/
- http://github.com/guyzmo/event-source-library/
- http://www.tornadoweb.org/