Ember Data, real-time, and websockets


#1

Does Ember Data using a localStorage adapter make sense for a real-time chat application using websockets or TCP sockets?

It makes more sense to me to just store data in memory in this case. In the event that a user opens multiple sockets, then the socket instance can easily be a part of the model.

Using Ember Data, the socket instance would be decoupled from the model/record to which it is pushing new messages. This means that in addition to retrieving the record from the store, the model hook also has to simultaneously obtain the associated socket instance so that they can both be passed to the controller.

Here is roughly what I mean:

Without Ember Data

var connections = [
  {
    server: "chat.something.com",
    messages: [],
    socket: {
      ondata: funcRef,
      onerror: funcRef
    }
  }
];

Using Ember Data

// model
server: DS.attr("string"), // chat.something.com
messages: DS.hasMany("message")

// socket service
var connections = [
  {
    socketFor: "chat.something.com",
    ondata: ... // find corresponding record and do pushPayload
  }
];

#2

I would use Ember Pouch for this.


#3

I’m not sure if Ember Pouch solves the problem of the socket instance being decoupled from the associated record since it is just an adapter for Ember Data.

The main question is does Ember Data really make any sense at all for this type of application? With other JavaScript frameworks, there would be no question that all incoming data would just exist in memory. Ember Data is entirely new concept to me, and I’m not sure if it would actually help me out or if it’s completely irrelevant for what I want to build. It’s just forcing me to rethink my model concept.


#4

Ember Pouch is not only an Adapter. It stores chat data in your browser and syncs only change. See http://exmer.com/bloggrcouch/ and wait for data slowly coming from iriscouch. After 2 minutes try add data in 2 browsers. Source


#5

Hi man! We are working for some lib for this, now we are finishing first part, online/offline mode, and next part will be socket support. you can look: https://github.com/api-hogs/ember-data-offline


#6

Thanks for letting me know about this. I ended up using temporary models that are stored in a service, but I am eventually looking to transition to Ember Data, so this will be useful for me.