-
-
Save pyzen/2c6dcba8e85a5f00d715 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> | |
<script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script> | |
<script> | |
$(function() { | |
var conn = new SockJS('http://localhost:8080/push'); | |
conn.onmessage = function(e) { | |
console.log('Got', e.data); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. pip install -r reqs.pip | |
2. server.py | |
3. open client.html in browser | |
4. redis-cli publish push '123456' | |
5. check browser console |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tornado>=2.2 | |
sockjs-tornado | |
toredis |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
import argparse | |
import tornado.ioloop | |
import tornado.web | |
import sockjs.tornado | |
import toredis | |
# Our sockjs connection class. | |
# sockjs-tornado will create new instance for every connected client. | |
class BrokerConnection(sockjs.tornado.SockJSConnection): | |
clients = set() | |
def on_open(self, info): | |
logging.info('Incoming client from %s' % info.ip) | |
self.clients.add(self) | |
def on_message(self, message): | |
logging.debug('Received something from client: %s', message) | |
def on_close(self): | |
self.clients.remove(self) | |
@classmethod | |
def pubsub(cls, data): | |
msg_type, msg_chan, msg = data | |
if msg_type == 'message': | |
logging.debug('Pushing: %s' % msg) | |
for c in cls.clients: | |
c.send(msg) | |
if __name__ == "__main__": | |
# Logging | |
import logging | |
logging.getLogger().setLevel(logging.DEBUG) | |
# Parse options. TODO: Use decent option parsing library. | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--endpoint', | |
default='/push', dest='endpoint', | |
help='SockJS URL endpoint') | |
parser.add_argument('--port', | |
type=int, default=8080, dest='port', | |
help='SockJS server port') | |
parser.add_argument('--key', | |
default='push', dest='key', | |
help='Redis key') | |
parser.add_argument('--redis_server', | |
default='localhost', dest='redis_server', | |
help='Redis host') | |
parser.add_argument('--redis_port', | |
default=6379, dest='redis_port', | |
help='Redis port') | |
v = parser.parse_args() | |
# Initialize tornado-redis and subscribe to key | |
rclient = toredis.Client() | |
rclient.connect(v.redis_server, v.redis_port) | |
rclient.subscribe(v.key, BrokerConnection.pubsub) | |
# Initialize sockjs-tornado and start IOLoop | |
BrokerRouter = sockjs.tornado.SockJSRouter(BrokerConnection, v.endpoint) | |
app = tornado.web.Application(BrokerRouter.urls) | |
app.listen(v.port) | |
logging.info('Listening on port %d for redis key %s', v.port, v.key) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment