Websocket API

Endpoint

wss://obniz.io/obniz/{obniz_id}/ws/1

before issue access token, you can connect above endpoint without it.

Below is Available QueryStrings.

Property(query) Default Description
access_token None An access token issued per device in Device Management. Or an API key or WebApp token or OAuth token with "device control" permission.

JSON

Use object array to send/receive Websocket API.

Connection

  1. Connect to the endpoint.
  2. If redirect directive received, you need to close current socket and open new socket to new endpoint.
  3. If ready received, connection was established.

Below is a simple example.

<!-- HTML Example -->
<html>
<body>
<script>
  // Simplest example
  var host = 'wss://obniz.io'
 
  function connect() {
    var socket = new WebSocket(host + '/obniz/OBNIZ_ID_HERE/ws/1');
 
    socket.onmessage = function (event) {
      var arr = JSON.parse(event.data);
      for (var i=0; i<arr.length; i++) {
        var obj = arr[i];
        if (obj.ws && obj.ws.redirect) {
          host = obj.ws.redirect;
          socket.onmessage = null;
          socket.close();
          connect();
        }
        if (obj.ws && obj.ws.ready) {
          socket.send(JSON.stringify([{display:{clear:true}}, {display:{text:"Works fine."}}]));
        }
      }
    }
 
  }
 
  connect();
 
</script>
</body>
</html>