// based on http://livsey.org/blog/2013/02/10/integrating-pusher-with-ember/ App.Faye = Ember.Object.extend({ init: function() { var _this = this; this.service = new Faye.Client('http://localhost:9292/faye'); // TODO: review http://faye.jcoglan.com/security/authentication.html and come up with a better solution this.client = 'web'; this.service.on('transport:down', function() { // the client is offline console.log('Server connection is down'); }); this.service.on('transport:up', function() { // the client is online console.log('Server connection is up'); }); this.service.subscribe('/clients/' + this.client, function(message) { console.warn("Got personal message!"); }); this.service.addExtension({ incoming: function(message, callback) { console.info('faye-in: ', message); if(message.data && message.data.eventName && message.data.data) { console.info("Got event!"); router = _this.get("container").lookup("router:main"); try { router.send(message.data.eventName, message.data.data); } catch (e) { unhandled = e.message.match(/Nothing handled the event/); if (!unhandled) { throw e }; } } callback(message); }, outgoing: function(message, callback) { console.info("faye-out:", message); callback(message); } }); }, subscribe: function(channel, handler) { return this.service.subscribe(channel, handler); }, publish: function(channel, message, meta) { meta = meta != undefined ? meta : {}; var payload = $.extend(meta, { client: this.client, data: message }); return this.service.publish(channel, payload); } }); Ember.ControllerMixin.reopen({ faye: null }); Ember.Application.initializer({ name: "faye", before: "store", initialize: function(container, application) { // use the same instance of Faye everywhere in the app container.optionsForType('faye', { singleton: true }); // register 'faye:main' as our Faye object container.register('faye:main', application.Faye); // inject the Faye object into all controllers and routes container.typeInjection('controller', 'faye', 'faye:main'); container.typeInjection('route', 'faye', 'faye:main'); application.inject('adapter', 'faye', 'faye:main'); } });