> ## Documentation Index
> Fetch the complete documentation index at: https://docs.metar.ws/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js

> A complete, reconnecting WebSocket client using the ws package.

Install `ws` and run the script below. It reconnects automatically on close and re-subscribes on every successful connection.

```bash theme={null}
npm install ws
```

```javascript theme={null}
import WebSocket from "ws";

const KEY = "mts_live_YOUR_KEY";
const CHANNELS = ["metar.obs.eglc", "metar.obs.rksi"];

function connect() {
    const ws = new WebSocket(
          `wss://stream.metar.ws/v1/ws?key=${encodeURIComponent(KEY)}`,
        );

    ws.on("message", (buf) => {
          const msg = JSON.parse(buf.toString());
          switch (msg.type) {
            case "ack":
                      console.log("connected:", msg.plan, msg.limits);
                      ws.send(JSON.stringify({ action: "subscribe", channels: CHANNELS }));
                      break;
            case "publication":
                      console.log(`${msg.data.station}: ${msg.data.temp_c}°C (${msg.data.report_time})`);
                      break;
            case "error":
                      console.error("error:", msg.code, msg.channel ?? "");
                      break;
          }
    });

    ws.on("close", () => setTimeout(connect, 3000)); // reconnect loop
    ws.on("error", (e) => console.error("ws error:", e.message));
}

connect();
```

<Note>
  On the Sandbox plan, subscribe to `sandbox.demo` instead and trigger a message with **Send test alert** on the Billing page in the portal.
</Note>

<CardGroup cols={3}>
  <Card title="Python" icon="python" href="/examples/python">
    The same client using websockets
  </Card>

  <Card title="Go" icon="code" href="/examples/go">
    The same client using gorilla/websocket
  </Card>

  <Card title="Browser" icon="window" href="/examples/browser">
    A minimal client for internal dashboards
  </Card>
</CardGroup>
