I tried to implement a model update in a (experimental) chat application. I used SSE : ActionController::Live on the server side (Ruby on Rails) and EventSource on the client side.
Simplified code:
App.MessagesRoute = Ember.Route.extend({ activate: function() { if (! this.eventSource) { this.eventSource = new EventSource('/messages/events'); var self = this; this.eventSource.addEventListener('message', function(e) { var data = $.parseJSON(e.data); if (data.id != self.controllerFor('messages').get('savedId')) { self.store.createRecord('message', data); } }); } } }); App.MessagesController = Ember.ArrayController.extend({ actions: { create: function() { var data = this.getProperties('body'); var message = this.store.createRecord('message', data); var self = this; message.save().then(function (response) { self.set('savedId', response.id); }); } } });
The logic is simple: I get every new entry from EventSource. Then, if the record was created by another client, the application detects it and adds a new record to the repository using ember-data createRecord . Suppose that this logic may have some reservations, but at least it serves as a โproof of conceptโ. Chat is working.
Full sources are available here: https://github.com/denispeplin/ember-chat/
I need to say something about rebooting: you probably don't want to perform a full reboot, this is a resource-intensive operation. However, your client side needs some way to find out about new entries. Thus, getting new records individually through SSE is probably the best option.
source share