Header_Audio_APIHeader_Audio_API

Develop apps to control Sony's latest home audio devices

Using WebSockets with Sony's Audio Control API

Here you'll find a tutorial to help you understand how to use WebSockets when working with the Audio Control API.

WebSocket example:

1Step 1: Requirements

To follow this example, you must have Node.js installed. For more information and instructions, see https://nodejs.org/en/ and https://en.wikipedia.org/wiki/Node.js.

We will also use the WebSocket Node.js library, which can be installed with npm.

2Step 2: Setup

  • Create a new folder, open a terminal, and go to the newly created directory.
{{=it.html}}gt; mkdir websocket_example
{{=it.html}}gt; cd websocket_example 
  • In the newly created directory initialize Node.js and accept defaults.
{{=it.html}}gt; npm init --yes
  • Install the WebSocket library.
{{=it.html}}gt; npm install websocket

3Step 3: Get playing content info notifications

  • Create a playing_content_info.js file, and include the following code:
  • Note: you will have to change the IP address and port number in the last line. To find the {ipaddress} and {port} number see X_ScalarWebAPI_BaseURL in Discovery Process, for example, for Home Audio products the port is usually 10000 and for Personal Audio products, 54480. The {ipaddress} is the IP address of the audio product.To get the playing content info notifications, you must enable the notifyPlayingContentInfo notification via the switchNotifications method.
#!/usr/bin/env node
var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();
function switchNotifications(id,disable,enable){
  return {
    "method": "switchNotifications",
    "id": id,
    "params": [{
      "disabled": disable,
      "enabled": enable
    }],
    "version": "1.0"
  }
}
client.on('connectFailed', function(error) {
  console.log('Connect Error: '   error.toString());
});
client.on('connect', function(connection) {
  console.log('WebSocket Client Connected');
  connection.on('error', function(error) {
    console.log("Connection Error: "   error.toString());
  });
  connection.on('close', function() {
    console.log('WebSocket Connection Closed');
  });
  connection.on('message', function(message) {
    if (message.type === 'utf8') {
      var msg = JSON.parse(message.utf8Data);
      // Check whether the message ID equals '1', to avoid creating a loop.
      if (msg.id == 1){
        let all_notifications = msg.result[0].disabled.concat(msg.result[0].enabled);
        var enable = [];
        var disable = [];
        // Enable only the 'notifyPlayingContentInfo' notifications.
        all_notifications.forEach(
          item => item.name == "notifyPlayingContentInfo"
          ? enable.push(item) : disable.push(item) );
        // Use a different ID than '1', to avoid creating a loop.
        connection.sendUTF(JSON.stringify(switchNotifications(2,disable,enable)));
      } else {
        console.log("Received: '"   message.utf8Data   "'");
      }
    }
  });
  function subscribe() {
    if (connection.connected) {
      // To get current notification settings, send an empty 'switchNotifications' 
      // message with an ID of '1'.
      connection.sendUTF(JSON.stringify(switchNotifications(1,[],[])));
    };
  }
  subscribe();
});
client.connect('ws://192.168.1.119:10000/sony/avContent');
  • Enter the following code in the terminal or command prompt to run the code.
{{=it.html}}gt; node ./playing_content_info.js
  • The output should be similar to the following:
WebSocket Client Connected
Received: '{"id":2,"result":[{"disabled":[{"name":"notifyAvailablePlaybackFunction","version":"1.0"},{"name":"notifyExternalTerminalStatus","version":"1.0"}],"enabled":[{"name":"notifyPlayingContentInfo","version":"1.0"}]}]}'
Received: '{"method":"notifyPlayingContentInfo","params":[{"contentKind":"input","output":"extOutput:zone?zone=1","source":"extInput:bd-dvd","uri":"extInput:bd-dvd"}],"version":"1.0"}'
Received: '{"method":"notifyPlayingContentInfo","params":[{"contentKind":"input","output":"extOutput:zone?zone=1","source":"extInput:sat-catv","uri":"extInput:sat-catv"}],"version":"1.0"}'
Received: '{"method":"notifyPlayingContentInfo","params":[{"contentKind":"input","output":"extOutput:zone?zone=2","source":"extInput:video?port=1","uri":"extInput:video?port=1"}],"version":"1.0"}'

Note: A new Received message should be posted in the terminal every time the input source is changed.