[Answered ]-Making two player chess app with Django– need player's board to update when opponent makes a move

1👍

Your first option that doesn’t require any sophisticated software is polling. Let say, that there is player A and player B playing chess. You can make client’s side of both player A and B ask server for update whenever certain period of time elapses. This isn’t the most optimalized solution, but it will work.

Something like:

function callServerAndWait500Milisecounds(){
   setTimeout(function(){
      $.ajax({
         url: "/your_url_to_server?playerId=" + playerId,
         method: "GET",
         success: function(data){
             //analize data from server and do something with it
         }
      });
   }, 500)
}
$(document).ready(function(){
    //code that fires when client side is loaded
    setTimeout(function(){callServerAndWait500Milisecounds();}, 500);
});

If you are using C# MVC for a server side, then you can use SignalR library. Link here: https://www.asp.net/signalr. This library allows both client side to call something on the server and most importantly it allows server to use a function on the client side written in javascript. This library is pretty amazing and it can do a lot of stuff for you like managing groups of users (If player A plays with player B and player C plays with player D then A should be able to communicate with B only and so on).

Hope it helps!

1👍

Use Django Channels it a officially Django project at https://github.com/django/channels

It’s a WebSockets extension to Django.

Websockets allows you to send a message to the clients without the client polling for it. There is a great examples of this at https://github.com/andrewgodwin/channels-examples.

Leave a comment