Creating simple Web application in JavaScript

This tutorial will guide you through the process of creating basic web application in JavaScript on top of xAPI platform using Websockets.

Commands used in this tutorial:

  • login,
  • getAllSymbols.

Before you start...

  • you should have at least basic understanding on WebSocket technology. If this is the first time you hear about Websockets we strongly encourage you to read this introduction: This should give you better understanding on the subject;
  • in this tutorial we will use jQuery framework, which can be downloaded from:, and Bootstrap from Twitter to beautify our app: You can find tons of tutorials about that frameworks on the web.

Step 1. Create basic layout.

  1. Create new HTML file. You may name it as you want, in our case it will be: index.html.
  2. Write initial layout. It is just a standard HTML5 webpage with Bootstrap imported. It is downloaded from Bootstrap webpage each time you load the page, so the Internet connection is required to view the page correctly. You may download the bootstrap.css file for yourself and include it from your local disk if you want.
  3. <!DOCTYPE html>
        <title>First xAPI WebApp</title>
          <link rel="stylesheet" href="" />
        <!-- Page body goes here -->

Step 2. Add controls to the layout

  1. Our page will consist of two text boxes: one for user id and one for password...
  2. <div class="row">
      <div class="col-md-4 col-md-offset-4">
        <h1>First xAPI WebApp</h1>
        <form role="form">
          <div class="form-group">
            <label for="userId">User id</label>
            <input type="text" class="form-control" id="userId" placeholder="User ID">
          <div class="form-group">
            <label for="password">Password</label>
            <input type="password" class="form-control" id="password" placeholder="Password">
          <div type="submit" id="login-btn" class="btn btn-primary">Log in</div>
  3. ... and a table to display list of symbols
  4. <div class="row">
      <div class="col-md-4 col-md-offset-4">
        <h3>Available instruments</h3>
        <table class="table">
          <tbody id="instruments">
            <!-- Instruments go here -->

Step 3. Add some logic

  1. First, include jQuery framework by inserting:
  2. <script src="" type="text/javascript"></script>
  3. Now we are ready to establish connection with xAPI. We will connect to demo websocket proxy at: wss:// The following code do the trick:

      // Our websocket
      var ws;
      function connect() {
        var url = "wss://";
        console.log('Connecting to: ' + url);
        ws = new WebSocket(url);
        ws.onopen = function() {
        ws.onmessage = function(evt) {
          console.log("Received: " +;
          try {
            var response = JSON.parse(;
            if(response.status == true) {
              if(response.streamSessionId != undefined) {
                // We received login response
              } else {
                // We received getAllSymbols response
            } else {
              alert('Error: ' + response.errorDescr);
          } catch (Exception) {
            alert('Fatal error while receiving data! :(');
        ws.onclose = function() {
          console.log('Connection closed');
      function disconnect() {
      function send(jsonMessage) {
        try {
          var msg = JSON.stringify(jsonMessage);
          console.log('Sent ' + msg.length + ' bytes of data: ' + msg);
        } catch(Exception) {
          console.error('Error while sending data: ' + Exception.message);
      function login() {
        var msg = {};
        msg.command = "login";
        var arguments = {};
        arguments.userId = parseInt($('#userId').val());
        arguments.password = $('#password').val();
        msg.arguments = arguments;
        console.log('Trying to log in as: ' + msg.arguments.userId);
      function getAllSymbols() {
        var msg = {};
        msg.command = "getAllSymbols";
        console.log('Getting list of symbols');
      function parseGetAllSymbols(returnData) {
        // For all symbols 
        for (var i = returnData.length - 1; i >= 0; i--) {
          var symbol = returnData[i].symbol;
          var ask = returnData[i].ask;
          var bid = returnData[i].bid;
          var descr = returnData[i].description;
          var row = '<tr><td>' + symbol + '</td><td>' + ask + '</td><td>' + bid + '</td><td>' + descr + '</td></tr>';
          // Append to instruments table
      $(document).ready(function() {
        $('#login-btn').on('click', function() {


As a result you should get a website that looks simillar to the one on the following screenshot.


Full code from this tutorial is available here: code.