Update
Demo: http://jsbin.com/ogorab/311/edit
I'm trying to build a simple chat room that updates in realtime using Faye/Websockets. Messages are posted using regular REST, but there is also a subscription via Faye to /messages/created, which uses store.pushPayload to push the new messages.
Now the following scenario happens and I can see where it goes wrong but I have no clue how to solve it:
- User submits chat message
ChatControllerhandles the submit, callscreateRecordwith the chat message, and subsequently #save- The chat messages is instantly shown in the chat (triggered by
createRecord). Note that noidhas been assigned yet. - A REST request is send to the server
- The server first publishes the message to Faye
- The server responds to the REST request
- Before the ajax call is resolved, a message has arrived at /messages/created
- The message is again inserted in the view (it should be merged with the original message of course, but that one still hasn't been assigned an id)
- The ajax call is resolved, and the id of the original message is assigned.
This results in duplicate messages, in the following order:
- [message via
createRecord, will resolve via ajax response] - [message inserted via
pushPayload/Faye]
I hope you can understand so far. A solution would be to have Faye wait for the save call to resolve before pushing the payload. Unfortunately I don't have a reference to the record that is being saved (happens in a controller, faye subscription is set up in ApplicationRouter).
Also I would like this to work in a generic way:)