Skip to content

Instantly share code, notes, and snippets.

@lukemelia
Created January 15, 2010 02:23
Show Gist options
  • Save lukemelia/277735 to your computer and use it in GitHub Desktop.
Save lukemelia/277735 to your computer and use it in GitHub Desktop.
WE.RealtimeNotificationListener = (function(){
var instanceMethods = {};
var staticMethods = {
listen: function(channel, notificationHandler) {
var realtimeNotificationsUrl = "http://" + location.host + "/rt/notifications?id=" + channel;
var realtimeNotificationsCookieName = 'channel_last_mod_' + channel;
var storedLastModifiedDate = $.cookie(realtimeNotificationsCookieName);
if (storedLastModifiedDate !== null) {
$.lastModified[realtimeNotificationsUrl] = storedLastModifiedDate;
}
$.ajax({
url: realtimeNotificationsUrl,
type: 'GET',
ifModified: true,
dataType: 'json',
complete: function(xhr, textStatus) {
if (textStatus === 'success') {
$.cookie(realtimeNotificationsCookieName,
xhr.getResponseHeader("Last-Modified"),
{ expires: 1});
}
},
success: function(data, textStatus) {
notificationHandler(data);
_.defer(function() { WE.RealtimeNotificationListener.listen(channel, notificationHandler); });
}
});
}
};
return Base.extend(instanceMethods, staticMethods);
})();
if Settings.nginx_push_notifications == :on
require 'curb'
end
class NginxPusher
SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXX'
PUBLISH_PATH = '/rt/publish'
def initialize(user_or_user_id)
if user_or_user_id.is_a?(User)
@user_id = user_or_user_id.id
else
@user_id = user_or_user_id.to_i
end
end
def channel
@channel ||= Digest::SHA1.hexdigest("#{SECRET}--#{@user_id}")
end
def publish(data)
if Settings.nginx_push_notifications == :on
c = Curl::Easy.new
nginx_instances.each do |nginx_instance|
c.url = "http://#{nginx_instance}#{PUBLISH_PATH}?id=#{channel}"
c.http_post(data)
end
end
end
def self.publish(json, user_ids)
online_user_ids = MemberPresenceTracker.online?(*user_ids)
online_user_ids.each do |user_id|
NginxPusher.new(user_id).publish(json)
end
end
def nginx_instances
Settings.nginx_hosts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment