Issue with /v1/client/allocation/:id/exec WebSocket endpoint (Error 500)

Hello everyone,

I’m trying to use Nomad’s WebSocket API endpoint to execute commands inside an allocation from a Node.js script. I have the raw_exec driver enabled, and the ACL token I’m using has proper permissions over the allocations.

Here is the code I’m using to test the WebSocket connection:

const WebSocket = require('ws');

const NOMAD_CLIENT_URL = 'ws://192.168.59.214:4646/v1/client/allocation/d02a61e8-a047-6d00-d2cb-013ae7b4940e/exec';

const ws = new WebSocket(NOMAD_CLIENT_URL, 'nomad-exec', {
  headers: {
    'X-Nomad-Token': '6dea2728-f507-5ea8-a48e-4439b701b20a',
  }
});

ws.on('open', () => {
  console.log('✅ Connected to Nomad WebSocket');

  const commandPayload = {
    Command: 'echo',
    Args: ['hi'],
    Tty: false,
    Task: 'server'
  };

  ws.send(JSON.stringify(commandPayload));
});

ws.on('message', (data) => {
  process.stdout.write(data);
});

ws.on('error', (err) => {
  console.error('❌ Error:', err.message);
});

ws.on('close', () => {
  console.log('\n🔚 Connection closed');
});

However, when I run it, I get the following error:

❌ Error: Unexpected server response: 500

🔚 Connection closed

Things I’ve already verified:

The command nomad alloc exec -task=server d02a61e8-a047-6d00-d2cb-013ae7b4940e echo hi works perfectly from the CLI.

The ACL token has proper write permissions for node and allocation.

The Nomad client has the raw_exec plugin enabled and is listening on the correct IP.

I’m connecting directly to the client node, not the Nomad server.

Is there any extra configuration I might be missing to get this WebSocket endpoint working properly?
I would really appreciate any suggestions or help from someone who has implemented this functionality successfully.

Thanks a lot in advance!