WebSocket Authentication

The cubestech WebSocket endpoint reads x-api-key from the opening HTTP upgrade request.

Node.js

The ws package supports custom headers.

bash
npm install ws
javascript
import WebSocket from "ws"

const socket = new WebSocket("wss://ws.sol.cubestech.online", {
  headers: {
    "x-api-key": process.env.NODE_API_KEY,
  },
})

socket.on("open", () => {
  socket.send(JSON.stringify({
    jsonrpc: "2.0",
    id: "logs",
    method: "logsSubscribe",
    params: ["all", { commitment: "confirmed" }],
  }))
})

socket.on("message", (data) => {
  console.log(JSON.parse(data.toString()))
})

Browser apps

The browser WebSocket API does not accept custom headers.

Use a backend relay:

  1. Your browser opens an authenticated connection to your backend.
  2. Your backend opens the cubestech connection with x-api-key.
  3. Your backend forwards only the events needed by the signed-in user.

Keep these controls on the relay:

  • User authentication
  • Per-user subscription limits
  • Allowed method list
  • Allowed account and program list
  • Message size limits
  • Idle connection cleanup

Never place your cubestech API key in browser JavaScript, a URL, or a query string.

Terminal tools

bash
wscat \
  -c wss://ws.sol.cubestech.online \
  -H "x-api-key: YOUR_API_KEY"