title: Websocket with kraken closes after sending subscription for open position
body: <p>I am trying to subscribe to the kraken websocket API for the OpenPositions. I have sent a challenge request and receive the challenge , i also have signed it correctly with my secret key but it seems like once i sent the openpositions subsctiption request, the web socket closes directly with a code of 1006 and reasons ''.</p>
  
  <p>I am writing in Nodejs using the native library websocket and event emiiter to emit data of "kraken-update"</p>
  
  <p>this is the main file that i run:</p>
  
  <pre><code>
  const con = new Connection(key = keys.api_key, secret=keys.secret);
  con.on('kraken-update', (data) =&gt; {
      console.log(new Date, "DATA", data);
      return;
  });
  const go = async(key, secret) =&gt; {
      console.log(new Date, 'connecting...');
      await con.connect();
      console.log(new Date, 'connected');
      await con.sendchallenge()
  }
  </code></pre>
  
  <p>and here is the Connection class</p>
  
  <pre><code>const WebSocket = require('ws');
  const crypto = require('crypto');
  const EventEmitter = require('events');
  
  class Connection extends EventEmitter {
      constructor( key, secret, challenge = '') {
          super();
          this.url = 'wss://futures.kraken.com/ws/v1';
          this.connected = false;
          this.challenge = challenge;
          this.lastMessageAt = 0;
          this.key=key;
          this.secret=secret;
      }
  
      disconnect() {
          this.ws.disconnect();
      }
  
      connect() {
          if(this.connected) {
              console.log("ALREADY CONNECTED")
              return;
          }
          let readyHook;
          this.onOpen = new Promise(r =&gt; readyHook = r);
          this.ws = new WebSocket(this.url);
          this.ws.onopen = () =&gt; {
              this.connected = true;
              readyHook();
          }
          this.ws.onerror = (e) =&gt; {
              console.log(new Date, '[KRAKEN] error', e);
          }
          this.ws.onclose = (e) =&gt; {
              console.log(new Date, '[KRAKEN] close', e);
          }
          this.ws.onmessage = (e) =&gt; {
              //console.log("MESSAGE ",e.data)
              setImmediate(()=&gt;this.handleMessage(e))
          }
  
          // initial book data coming in on the same tick as the subscription data
          // we defer this so the subscription promise resloves before we send
          // initial OB data.
          return this.onOpen;
      }
  
      handleMessage(e){
          this.lastMessageAt = new Date;
          console.log(e)
          const payload = JSON.parse(e.data);
          if(payload.event === 'subscribed') {
              this.emit('kraken-update', payload)
              console.log(new Date, "SUBSCRIBED ",payload)
              return;
          }
          if (payload.event === 'challenge'){
              this.challenge = payload.message
              this.emit('kraken-update', payload.message);
              this.subscribeOpenPositions();
              return;
          }
          if (payload.feed === "open_positions"){
              this.emit('kraken-update', payload.message);
              return;
          }
  
          else{
              console.log(new Date, "UNDETECTED ",payload)
              return;
          }
      }
  
      sendchallenge(){
          try{
              let onReady = new Promise(e =&gt; e);
              const data = JSON.stringify({'event': 'challenge','api_key':this.key})
              this.ws.send(data);
              return onReady;
          }
          catch(e){
              console.log(new Date, 'ERROR SENDING CHALLENGE')
              throw(e)
          }
      }
  
      subscribeOpenPositions(){
          //how to sign challenge:
          //https://support.kraken.com/hc/en-us/articles/360022635652-Sign-Challenge-Web-Socket-API-
          console.log(new Date, `SUBSCRIBING OPENPOSITION WITH ${this.challenge}....`);
          try{
              const secret_buffer = new Buffer.from(this.secret, 'base64');
              const hash          = new crypto.createHash('sha256');
              const hmac          = new crypto.createHmac('sha512', secret_buffer);
              const hash_digest   = hash.update(this.challenge).digest('binary');
              const hmac_digest   = hmac.update(hash_digest, 'binary').digest('base64');
              this.ws.send(JSON.stringify({"event":"ping"}))
              this.ws.send(JSON.stringify(
                  {"event":"subscribe",
                  "feed":"open_positions",
                  "api_key":this.key,
                  "original_challenge":this.challenge,
                  "signed_challenge":hmac_digest}
              ));
              console.log("SENT");
          }
          catch(e){
              console.log(new Date,"ERROR SENDING SUBSCRIPTION REQUEST")
              throw(e)
          }
      }
  }
  
  module.exports = Connection;
  </code></pre>
  
  <p>summary of what happened:</p>
  
  <p>connnect to kraken future server => request challenge => handle challenge then request openorder subscription while handling challenge => websocket closes</p>