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.

プロパティ(query)デフォルト値説明
access_tokenなしアクセストークン設定済みデバイスの場合に必要です。

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>