# Ember Data, Rest, Socket.IO, something like a race condition

**URL:** <https://discuss.emberjs.com/t/ember-data-rest-socket-io-something-like-a-race-condition/10536>\
**Category:** Ember Data\
**Created:** [April 25, 2016, 5:12pm UTC](https://discuss.emberjs.com/t/ember-data-rest-socket-io-something-like-a-race-condition/10536 "2016-04-25T17:12:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![OakBehringer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/oakbehringer/32/11913_2.png) [@OakBehringer](https://discuss.emberjs.com/u/OakBehringer)\
**Post date:** [April 25, 2016, 5:12pm UTC](https://discuss.emberjs.com/t/ember-data-rest-socket-io-something-like-a-race-condition/10536/1 "2016-04-25T17:12:00Z")

</div>

I am working on a web application that uses a mix of Ember’s rest adapter and [socket.io](http://socket.io). I have a few models that receive updates on our backend (outside of the Ember/client relationship), whose changes need to be pushed to Ember, immediately, thus the socket connection.

I have written an Ember service that watches for a few socket events, which are pretty simple: They push up a payload of data, representing a model (in the same format as the rest api), and an action (Create, Update, or Delete). The service then uses Ember’s store to perform the appropriate action:

1. Delete event from socket: If peekRecord returns a record for the deleted ID, it uses store.unloadRecord(…) to remove the record
2. Create or Update event from socket: store.pushPayload(…) is used to upsert the payload.

This is working great! However…

The problem I’m running into is… if I use the _store_ to create a new record, and model.save() to persist it… When I call model.save() (makes an HTTP Post request), the socket connection returns the new record before the rest api, uses pushPayload to push the new record into store _before_ save updates the record’s ID. The result is two records in Ember’s data store with the same ID. The process goes like this:

1. let widget = store.createRecord(‘widget’, { label: ‘My Widget’ });
2. widget.save();
3. // widget is HTTP posted to rest api
4. // widget is created in database
5. // database event triggers socket event, pushing new widget payload back to client: {widgets: [{ id: 69, label: ‘My Widget’ }]}
6. My aforementioned service runs store.pushPayload({widgets: [{ id: 69, label: ‘My Widget’ }]}); // new widget record is created in store
7. // original HTTP post request returns with status 201, {widget: { id: 69, label: ‘My Widget’ }}
8. store updates widget record created in step 1 to reflect id = 69

Store now has two records, representing the same object (widget with id of 69).

Does anyone have any advice or suggestions as to how I can get around this issue?

Thanks! Adam
