Last active
August 29, 2015 14:15
-
-
Save martincarlin87/951189121b45f5394dbc to your computer and use it in GitHub Desktop.
Pusher
This file contains 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
var handlePusher = function () { | |
var pusher = new Pusher([APP_KEY]); | |
var socket_id = null; | |
pusher.connection.bind('connected', function() { | |
socket_id = pusher.connection.socket_id; | |
}); | |
// Since it is a presence channel the name must be prefixed with presence- | |
var channel = pusher.subscribe('presence-ticket_' + ticket_ID); | |
channel.bind('editing', function(data) { | |
toastr.warning(data); | |
}); | |
$('#content').focusin(function() { | |
// trigger Pusher notification | |
var channel = 'presence-ticket_' + ticket_ID; | |
var event = 'editing'; | |
var message = 'Ticket ' + ticket_ID + ' is currently being edited by ' + logged_in_username; | |
var url = window.location.href; | |
// save event in the database | |
$.ajax({ | |
type: 'POST', | |
url: '/events/create', | |
data: { channel: channel, event: event, message: message, url: url, socket_id: socket_id }, | |
success: function(data) { | |
var event_id = data; | |
// send notification to Pusher | |
$.ajax({ | |
type: 'POST', | |
url: '/application/new_pusher_notification', | |
data: { channel: channel, event: event, message: message, socket_id: socket_id, event_id: event_id } | |
}); | |
} | |
}); | |
}); | |
} | |
// Rails method | |
def new_pusher_notification | |
require 'pusher' | |
Pusher.app_id = APP_ID | |
Pusher.key = APP_KEY | |
Pusher.secret = APP_SECRET | |
Pusher.trigger(params[:channel], params[:event], params[:message], { | |
:socket_id => params[:socket_id], | |
:event_id => params[:event_id] | |
}) | |
render :text => '1' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment