Skip to content

Instantly share code, notes, and snippets.

@tarnacious
Created May 28, 2014 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarnacious/403f5311cf62e9144fa6 to your computer and use it in GitHub Desktop.
Save tarnacious/403f5311cf62e9144fa6 to your computer and use it in GitHub Desktop.
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
r.publish('tweets',tweet_json)
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()
@tarnacious
Copy link
Author

tornado
tornado-redis
redis

@tarnacious
Copy link
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