Realtime layer on top of a REST adapter using Pusher

Hello guys,

I built an Ember App using EmberJS for the frontend that is backed by a beautiful Laravel RESTful API.

So good so far,

I found that on top or the current mecanisms I got, I wanted to sync data in realtime between differents users of my system using Pusher [ http://pusher.com/ ] .

I wrote down the socket logic in my ApplicationRoute .

I have 3 channels that receives the 3 types or interactions that are possible with entities : create, update, delete.

The contract I established for the message that are sent to my Ember app goes like this :

{
   model: "post" // model name
   token: "4ge4g3brth" // refer to the instance that sent the action ( explained in the code)
   data: {...} // the model's data
}

So the logic in my channels that I implemented is used to properly inject the data in the store. I’m posting it here because I don’t master Ember-Data so I’m sure my code could be better.

So please, take a look at the Gist below :

https://gist.github.com/pboutin/b6d4a3209c8eb56e9c9b#file-emberjs_pusher-js

Don’t hesitate to poke me if something is not clear, I commented a lot the code so it could be readable by everybody.

I feel socket code should be kept in the adapter vs the route

I’m looking to move the whole logic, the problem I’m facing right now, is if a new model is created by another instance, yes we must load it and insert it into the DataStore, but depending on “where” I am in the app, I could need to add it into the controller’s content.

Your models should be references of the model, so if the model object changes which is located in the store the page should change aswell, take a look at emberfire they do this quite well