Skip to content

Websocket Notification 'API'

Anthony Beaumont edited this page Mar 4, 2021 · 2 revisions

Websocket Notification 'API'

When enabled (default) Achievement Watcher's Watchdog will broadcast achievement unlock and progress data to all connected websocket clients.

A common usage example of this feature is for Streaming purposes (Twitch, etc ...) by creating your own notification in an OBS browser source.

API

Endpoint: ws://localhost:8082

On achievement unlock / progression the following JSON data will be send :

{
      appID: 480 //unique game identifier,
      game: "Spacewar" //game name,
      achievement: "ACH_WIN_ONE_GAME" //achievement id (api name),
      displayName: "Winner" //achievement title,
      description: "Win one game." //achievement description (if any),
      icon: "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/apps/480/winner.jpg" //unlocked icon url,
      time: 1614395797110 //unix timestamp,
      progress: //if it's a progress and not an unlocked achievement; 
                //Otherwise this property is not sent at all
      { 
          current: 20 //current progress,
          max: 100 //max progress value
      }
}  

Data is broadcasted to all connected websocket clients.
Websocket timeout: there is a 30sec ping/pong but normally it's handled by the browser so you shouldn't have to add code for it.

Example

const ws = new WebSocket("ws://localhost:8082"); //First connect to the ws endpoint

ws.onmessage = (evt) => { //Receive achievement
    const data = JSON.parse(evt.data); //Decode JSON response
    if(data.progress) return; //Do something only on achievement unlock
    //Do something with data
};

That wasn't difficult was it ? :D

To help you there is a 'test' command to send a dummy valid notification.

//Dummy test example.

const ws = new WebSocket("ws://localhost:8082"); //First connect to the ws endpoint

ws.onopen = (evt) => { //When connected do something
  ws.send( //Send something
    JSON.stringify({ //Data is exchanged via JSON
      cmd:"test", 
      broadcast: true //By default the dummy is only send to the client making the request (broadcast: false) 
                      //Use broadcast: true if you need to send the dummy to all connected clients
    })
  );
};

ws.onmessage = (evt) => { //Receive dummy response
  const data = JSON.parse(evt.data); //Decode JSON response
  console.log(data);
  ws.close(); //Close connection
};

Problems you can expect to encounter

  • Some achievements may not have any description available

  • Sometimes you can receive more than one achievement in the same time or multiple in a very short time frame.

    Handle such case as you see fit :

    • make a queue system
    • display them in cascade
    • don't and override previous 🙃
    • ...
  • Don't forget to handle error(s)

    For simplicity I have omitted any error handling in my code snippets. But for example :

    • Websocket errors (onerror) and failure (init).
    • JSON.parse will throw a SyntaxError if the string to parse is not valid JSON.

Basic template

Here is a 'basic' template with a ps4 inspired theme to get you going.

//TODO: finish me