> For the complete documentation index, see [llms.txt](https://wingbits.gitbook.io/wingbits-ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wingbits.gitbook.io/wingbits-ai/build/code-examples.md).

# Code examples

Minimal working examples for consuming the Wingbits live stream. All use the same server and `.creds` file. Replace `<YOUR_ASSIGNED_SUBJECT>` with the exact subject in your onboarding email, including its final wildcard.

Scoped trial credentials are normally subscribe-only. These examples use a direct Core NATS subscription and do not use request/reply, `_INBOX.*`, or JetStream APIs. See [Real-time stream (NATS)](https://wingbits.gitbook.io/wingbits-ai/build/nats-stream.md) for the full walkthrough and [Access & authentication](https://wingbits.gitbook.io/wingbits-ai/build/access-and-auth.md) for credentials.

## Python

```python
import asyncio
import nats

async def main():
    nc = await nats.connect(
        "nats://nats-cluster.wingbits.com:4222",
        user_credentials="prod-<CUSTOMER>-trial.creds",
    )
    subject = "<YOUR_ASSIGNED_SUBJECT>"

    async def handler(msg):
        print(msg.subject, msg.data.decode())

    await nc.subscribe(subject, cb=handler)
    await asyncio.Event().wait()

asyncio.run(main())
```

Install: `pip install nats-py`

## Node.js

```javascript
import { connect, credsAuthenticator } from "nats";
import { readFileSync } from "fs";

const nc = await connect({
  servers: "nats://nats-cluster.wingbits.com:4222",
  authenticator: credsAuthenticator(readFileSync("prod-<CUSTOMER>-trial.creds")),
});

const subject = "<YOUR_ASSIGNED_SUBJECT>";
const sub = nc.subscribe(subject);
for await (const m of sub) {
  console.log(m.subject, m.string());
}
```

Install: `npm install nats`

## .NET

```csharp
using NATS.Client.Core;
using NATS.Net;

var opts = NatsOpts.Default with
{
    Url = "nats://nats-cluster.wingbits.com:4222",
    AuthOpts = NatsAuthOpts.Default with
    {
        CredsFile = "prod-<CUSTOMER>-trial.creds",
    },
};

const string subject = "<YOUR_ASSIGNED_SUBJECT>";

await using var nc = new NatsClient(opts);
await foreach (var msg in nc.SubscribeAsync<string>(subject))
{
    Console.WriteLine($"{msg.Subject} {msg.Data}");
}
```

Install: `dotnet add package NATS.Net`

## Other languages

NATS has official clients for [most languages](https://docs.nats.io/using-nats/developer) (Go, Rust, Java, C#, …). The pattern is always the same: connect with your `.creds` file and subscribe directly to your assigned subject.

## Production consumer requirements

The examples above prove connectivity and intentionally print each message. For production, replace that per-message work with a non-blocking handoff and handle connection events explicitly. The pattern is:

```
on_message(message):
    if not bounded_queue.try_add(message):
        record_overload_and_alert()

worker_loop():
    message = bounded_queue.take()
    write_to_database_or_call_api(message)

on_slow_consumer_or_message_dropped(event):
    record_and_alert(event)

on_disconnect_or_reconnect(event):
    record_and_alert(event)
```

Choose and document what your application does when its bounded queue is full. Do not silently replace it with an unbounded queue or block the NATS read loop.

Keep the NATS message-reading loop responsive. The subscription handler should do only the minimum work needed to validate or decode a message and place it on a bounded internal queue. Run database writes, file writes, API calls, and other slower processing in separate workers.

Monitor queue depth, processing latency, pending and dropped messages, slow-consumer events, and disconnect/reconnect events. Configure automatic reconnect and an asynchronous error or status handler. If your client does not drain the connection quickly enough, Wingbits may close it to protect the latency and performance of the shared live service. Data missed during a disconnect is not replayed.

See [Keep your consumer healthy](https://wingbits.gitbook.io/wingbits-ai/build/nats-stream.md#keep-your-consumer-healthy) for the complete production checklist.

See available subjects in [NATS subjects & topics](https://wingbits.gitbook.io/wingbits-ai/reference/nats-subjects.md).
