Skip to content

WebSocket API

Konstantin Cyber edited this page Nov 26, 2018 · 73 revisions

KeyChain contains a WebSocket API. This API can be used to stream information from a KeyChain instance to any client that implements WebSockets. Implementations in different languages:

  • JavaScript
  • Python
  • JavaScript/HTML
  • etc

Connect your WebSocket implementation to ws://localhost:8123/api/websocket.

Message format

Each WebSocket API message is a JSON serialized object containing a command with the corresponding parameters.

For full comprehensive descriptions of the commands, acceptable parameters and values, go to the Protocol.

WebSocket integration guide

JavaScript integration

Before you proceed with the integration, you need to download and install KeyChain.

After installing KeyChain you will need to create a web page that connects to WebSocket through localhost.

Below is an example of how to write a simple HTML page in JavaScript. [Write our own example in JS]

<!DOCTYPE html>
<html>
  <head>
    <title>websocketd count example</title>
    <style>
      #count {
        font: bold 150px arial;
        margin: auto;
        padding: 10px;
        text-align: center;
      }
    </style>
  </head>
  <body>
    
    <div id="count"></div>
    
    <script>
      var ws = new WebSocket('ws://localhost:8080/');
      ws.onopen = function() {
        document.body.style.backgroundColor = '#cfc';
      };
      ws.onclose = function() {
        document.body.style.backgroundColor = null;
      };
      ws.onmessage = function(event) {
        document.getElementById('count').textContent = event.data;
      };
    </script>
    
  </body>
</html>

Open this page through localhost: ws://localhost:8123/api/websocket

Python integration

Requirements

Python ≥ 3.6

NB: This documentation is written for Python ≥ 3.6.

1. Install websocket with:

Pip install websockets

Response example

Successfully installed websockets-7.0