<p>I'm having a very hard time sending commands to an interactive bash shell using a <a href="https://github.com/websockets/ws" rel="nofollow noreferrer">node.js websockets</a> client. I'm running a cluster with kops and I'm able to establish the connection with a pod and get its prompt but I am unable to send commands and receive a response.</p>

<p>So my connection looks like:</p>

<pre><code>const WebSocket = require('ws');
const fs = require('fs');
const readline = require('readline');
const unescape = require('querystring').unescape;
const escape = require('querystring').escape;

const channel = '0';
const access_token = "[the_api_token]";
const pod_name = 'nginx-726417742-s1nv2';
const host_address = 'wss://[the_api_url]';
const cmd = `${escape('/bin/bash')}&amp;command=-i`;
const params = `stdout=1&amp;stdin=1&amp;stderr=1&amp;tty=1&amp;container=nginx&amp;command=${cmd}`;
const url = `${host_address}/api/v1/namespaces/default/pods/${pod_name}/exec?${params}`;
const options = {
  headers: {
    'Authorization': `Bearer ${access_token}`
  },
  ca: fs.readFileSync('ca.crt'),
}
const ws = new WebSocket(url, options);
ws.on('open', () =&gt; {
  console.log('connected');
});
ws.on('message', (data, flags) =&gt; {
  process.stdout.write(data);
});
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});
rl.on('line', function (cmd) {
  const data = channel + cmd;
  if (ws &amp;&amp; ws.readyState === 1) {
    ws.send(data);
  }
});
</code></pre>

<p>The <code>ws.on('message', (data, flags)</code> part prints the shell prompt perfectly <code>root@nginx-726417742-s1nv2:/#</code> but then I can type whatever and it goes trough <code>ws.send(data)</code> but no message is ever received and no error is generated. I have already tried to convert the data to base64 and send char by char instead of the whole line, but the result and behaviour is always the same.</p>

<p>If I proxy the API through <code>kubectl proxy --disable-filter=true --port=8080</code> and use <a href="https://github.com/kubernetes-ui/container-terminal" rel="nofollow noreferrer">https://github.com/kubernetes-ui/container-terminal</a> pointing to <code>ws://localhost:8080</code> and using <code>/api/v1/namespaces/default/pods/${pod_name}/exec</code> I can get a working terminal, but local proxying is not an option for me.</p>

<p>Any help will be much appreciated.</p>

<p>I know that my answer comes late. But maybe I can help someone else with this. I have noticed that often the Kubernetes API requires to set an origin when using websockets. Otherwise the API returns strange errors sometimes. In your case you could try to complete your options as follows:</p>
<pre><code>const options = {
  headers: {
    'Authorization': `Bearer ${access_token}`
  },
  ca: fs.readFileSync('ca.crt'),
  origin: 'https://&lt;the_api_url&gt;:&lt;port&gt;'
}
</code></pre>
<p>Furthermore I think that it is not required to set <code>wss</code> as protocol (in your <code>host_address</code> variable). Usually the protocol should be automatically upgraded from <code>https</code> to <code>wss</code> during the connection process.</p>
