Handling Socket.io events


#1

I’m trying to figure the best way out to handle Websocket (Socket.IO) updates on my client.

My current code looks like that:

  App.PostsRoute = Ember.Route.extend({
    activate: function(){
      var self = this;

      socket.on('newPost', function(data) {
        self.get('store').push('post', data);
      });

      socket.on('updatePost', function(post) {
        self.get('store').update('post', post);
      });

      socket.on('deletePost', function(post) {
        var post = self.get('store').find('post', post);
        self.get('store').deleteRecord(post);
      });
    },...

The data object is JSON from a Node backend.

Adding new posts works just fine by invoking push on the store. New items just appear in my views. However, the updatePost event causes a new post being added to the store. Deleting didn’t work at all for me. I just noticed that ember-data tries to access my backend at /api/posts?post_id=... which confuses me.

Besides those practical issues: Would it be a better solution to propagate only IDs of those objects that changed throught the websocket in general?

Any hints or any help is really appreciated. Thanks in advance.