Q:

Ember Data Adapter with Web Sockets

I am implementing a custom adapter for ember data which works with web sockets. My current implementation of the web socket is like this:
// create socket
var socket = new WebSocket('ws://' + host + '/' + path);
this.set('socket', socket);

socket.onopen = function () {
    // opened
};

socket.onmessage = function (message) {
    // message
};

sendMessage: function(message) {
    this.get('socket').send(message);
}

And then I have an adapter, which has to implement several functions according to ember data documentation (find(), findAll(), createRecord()...). All these functions are expected to return some value, for example an array. This way I can say this.store.find('...') and immediately get back some entities.
But with sockets you have some asynchronous way of data retrieval. How do you handle this? For example: The find() function of my adapter can send a message via sendMessage() in order to get an entity. But it can't return the entity immediately because the answer (= entity) is sent back via socket.onmessage. So is there are way to handle this properly?

A:

Actually, none of the Ember-Data adapters return objects immediately. How would they? REST is asynchronous too. :) What they're returning are proxies for objects. Specifically, DS.PromiseObject and DS.PromiseArray. They allow you to work with the objects as if they were real, and as soon as the asynchronous promise resolves, the bindings get updated automatically.
So here's a quick example (somewhat pseudocode):
find: function(store, type, id) {
    var promise = new Ember.RSVP.Promise(function(resolve) {
        socket.emit('get data', function(data) {
            resolve(data);
        });
    });

    return DS.PromiseObject.create({
        promise: promise
    });
}

This is how the built-in adapters handle things, so this will exhibit the exact same behavior that you've been seeing with the other adapters.

