Created
May 28, 2014 16:29
-
-
Save tarnacious/403f5311cf62e9144fa6 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
| import redis | |
| r = redis.StrictRedis(host='localhost', port=6379, db=0) | |
| r.publish('tweets',tweet_json) |
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
| from tornado import websocket, web, ioloop | |
| import tornadoredis | |
| c = tornadoredis.Client() | |
| c.connect() | |
| client_list = [] | |
| class SocketHandler(websocket.WebSocketHandler): | |
| def open(self): | |
| if self not in client_list: | |
| client_list.append(self) | |
| def on_message(self, message): | |
| pass | |
| def on_close(self): | |
| if self in client_list: | |
| client_list.remove(self) | |
| def on_message(m): | |
| if m.kind == "message": | |
| for client in client_list: | |
| client.write_message(m.body) | |
| def on_subscribed(s): | |
| c.listen(on_message) | |
| app = web.Application([ | |
| (r'/tweetstream', SocketHandler), | |
| ]) | |
| app.listen(8888) | |
| c.subscribe('tweets', callback=on_subscribed) | |
| ioloop.IOLoop.instance().start() |
Author
Author
$(function() {
var ws = new WebSocket('ws://' + window.location.hostname + '/tweetstream');
ws.onopen = function(){
ws.send("hello");
console.log("Open")
};
ws.onmessage = function(ev){
tweet = JSON.parse(ev.data);
container = $("<div/>");
$(".tweets").prepend(container.append("@" + tweet.user.screen_name + ": " + tweet.text))
};
ws.onclose = function(ev){
console.log("Closed")
};
ws.onerror = function(ev){
console.log("Error")
};
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tornado
tornado-redis
redis