API for Notifications about new moves

Hello!
Can’t find any info about possibility to use API to get notifications about new moves in correspondence games.
Is it possible and how?
Thanks.

There is none. I had to implement everyhing myself for the Android client… basically you have a local database on the phone and remember the games in progress in that database. Then you poll the server on a schedule (more often if the phone is on the charger, less if on battery) and compare the state you get from the server to the one in the database. When you see changes, you manually spawn notifications. It’s a huge amount of work, let me tell you that :expressionless:

2 Likes

Oh, that’s pity. It such an obvious, simple and useful thing!
Hope developers implement it soon.

I think you can request notifications over the realtime api.

The OGS overview page updates the turn indicator in real time. So there has to be a way without pulling the whole game stack every second.
I took a look at the OGS frontend code and it realy uses the realtime api.


To get notifications emit notification/connect with userid and auth-token (notification_auth).
After that you get push notifications if a game changes. The notififcations your are looking for are active_game, which notifies you that the state of one of your games changed.
If phase=play and player_to_move=your_player_id it’s your turn.

You get the notification_auth-token via https://online-go.com/api/v1/ui/config.

OGS source code

https://github.com/online-go/online-go.com/blob/devel/src/components/Notifications/Notifications.tsx

line 299 and following

        comm_socket.on("connect", () => {
            comm_socket.send("notification/connect", {"player_id": this.user.id, "auth": this.auth});
        });
        comm_socket.on("disconnect", () => {
            //console.log("Notifier disconnected from " + server);
        });
        comm_socket.on("active_game", (game) => {
            delete this.boards_to_move_on[game.id];

            if (game.phase === "stone removal") {
                if ((game.black.id === data.get("user").id && !game.black.accepted)
                    || (game.white.id === data.get("user").id && !game.white.accepted)
                ) {
                    this.boards_to_move_on[game.id] = game;
                }
            }
            else if (game.phase === "play") {
                if (game.player_to_move === data.get("user").id) {
                    this.boards_to_move_on[game.id] = game;
                }
            }

            if (this.boards_to_move_on[game.id]) {
                let current_game_id = getCurrentGameId();
                if ((current_game_id !== game.id || !document.hasFocus())) {
                    if (game.avg_move_time > 3600) { // don't notify for realtime games ever
                        emitNotification(_("Your Turn"), interpolate("It's your turn in game {{game_id}}", {'game_id': game.id}),
                            () => {
                                if (window.location.pathname !== "/game/" + game.id) {
                                    browserHistory.push("/game/" + game.id);
                                }
                            }
                        );
                    }
                }
}
1 Like

You could, but then the battery on the phone would last you for an hour and matburt would probably ban you because you’re flooding the server :slight_smile:

I am no programmer but would this help? the small number in the bracket shows how many moves are waiting for you. sure beats going thru the database.

image

1 Like

OK, I think something got lost in translation.

:+1:

That’s what I wanted to say.

Yes.

Using the real-time API. The documentation of the notifications is missing on https://ogs.readme.io/docs/real-time-API.

  1. You connect to https://onlien-go.com/socket.io using socket.io

    var socket = io.connect("https://online-go.com/socket.io/?EIO=3&transport=websocket");
    
  2. You connect to the notification channel with

    socket.emit("notification/connect", {player_id: 449941, auth: "abc123"});
    

    You get the auth token (notification_auth) on Ui Config – Django REST framework

  3. happily receive event messages active_game

    {
        'id': 16493434, 
        'phase': 'play', 
        'name': 'Tournament Game: Alan Turing Main Title Tournament 2017 (27492) R:3 (flovo vs linokai)', 
        'player_to_move': 1323, 
        'width': 19, 
        'height': 19, 
        'move_number': 12, 
        'paused': 0, 
        'private': False, 
        'black': {
            'username': 'linokai', 
            'id': 1323, 
            'rank': 15, 
            'professional': False, 
            'accepted': False, 
            'ratings': {
                'overall': {
                    'deviation': 101.98799654785262, 
                    'rating': 1209.1072695724488, 
                    'volatility': 0.05986201228119671, 
                    'games_played': 332}}}, 
        'white': {
            'username': 'flovo', 
            'id': 449941, 
            'rank': 19, 
            'professional': False, 
            'accepted': False, 
            'ratings': {
                'overall': {
                    'deviation': 73.03460718572202, 
                    'rating': 1575.5163497762592, 
                    'volatility': 0.060722367956637706, 
                    'games_played': 1345}}}, 
        'time_per_move': 93120}
    

    There are 2 interesting fields in the push notification:

    • phase: "stone removal"
      • if the field accepted of your colour is False, it’s your turn to accept the score.
      • if both players accepted the score you get another push notification with accepted: True for both players.
    • phase: "play"
      if player_to_move is your player_id it is your turn.

PS: After connecting to the notification channel, you instantly get a active_game notification for each of your active games.

2 Likes