Search code examples
restbackbone.jswebsocketchess

How do I implement chess with backbone.js with a RESTful api?


When one player makes a move that is sent to the server. And that move is pushed by the server to the second player. As far as I know, the server pushing the move to the second player goes against being a RESTful api.

From what little I know about backbone.js it is meant really for RESTful setups. Is there a way to use backbone.js with websockets to allow the server to push data down to the clients at any time?

Is there even an idiomatic way of implementing chess with backbone.js and websockets? And if not then what would be the correct way to implement chess?


Solution

  • You can definitely do it. Instead of fetching your collection/model, you will just set or update/reset the json data from the websocket into the proper model or collection.

    Somewhat pseudo-code example:

    var board = new Backbone.Collection(); // this would probably be your own extended Collection instead.
    
    function boardChange(jsonFromServer){
       // Take the json array from server,
       // and update the collection with it.
       // This would trigger 'change' events in each model in the collection (if changed).
       board.update(jsonFromServer);
    }