# Code Samples

This section provides working code examples for connecting to the TRADE RabbitMQ feed in various programming languages.

## Available Samples

| Language                                                                                       | File                  | Description                             |
| ---------------------------------------------------------------------------------------------- | --------------------- | --------------------------------------- |
| [C#](https://docs.lsports.eu/u/trade/integration/code-samples/rmq-consumer-c)                  | .NET Core consumer    | Complete consumer with message handling |
| [Node.js](https://docs.lsports.eu/u/trade/integration/code-samples/rmq-consumer-node.js)       | JavaScript/TypeScript | Event-based consumer using amqplib      |
| [Python](https://docs.lsports.eu/u/trade/integration/code-samples/rmq-consumer-python)         | Python 3.x            | Consumer using pika library             |
| [PHP](https://docs.lsports.eu/u/trade/integration/code-samples/rmq-consumer-php)               | PHP 7.4+              | Consumer using php-amqplib              |
| [GO](https://docs.lsports.eu/u/trade/integration/code-samples/rmq-consumer-go)                 | Golang                | Consumer using streadway/amqp           |
| [JavaScript](https://docs.lsports.eu/u/trade/integration/code-samples/rmq-consumer-javascript) | Browser/Node          | Basic JavaScript implementation         |

## Prerequisites

Before running any code sample, ensure you have:

1. **Valid credentials** — Username, password, and Package ID provided by your CSM
2. **Enabled distribution** — Call the Distribution/Start API endpoint
3. **Network access** — Ability to reach the InPlay and PreMatch RMQ hosts listed below

## Common Configuration

Connect using the username and password provided by your CSM. The host and virtual host depend on the feed type:

| Parameter        | InPlay                  | PreMatch                  |
| ---------------- | ----------------------- | ------------------------- |
| **Host**         | `stm-inplay.lsports.eu` | `stm-prematch.lsports.eu` |
| **Virtual Host** | `StmInPlay`             | `StmPreMatch`             |

| Parameter    | Value                                 |
| ------------ | ------------------------------------- |
| **Port**     | `5672`                                |
| **Queue**    | `_YOUR_PACKAGE_ID_` (e.g. `_102030_`) |
| **Protocol** | AMQP 0.9.1                            |

## Message Flow

```
┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│   TRADE Feed    │────▶│    RabbitMQ     │────▶│  Your Consumer  │
└─────────────────┘     └─────────────────┘     └─────────────────┘
                              │
                              ▼
                        Your Queue
                       _PACKAGE_ID_
```

## Best Practices

1. **Enable automatic recovery** - Handle connection drops gracefully
2. **Process messages asynchronously** - Don't block the message loop
3. **Implement proper logging** - Track message flow and errors
4. **Monitor heartbeats** - Detect feed issues early
5. **Use prefetch** - Control message buffer size

## Need Help?

If you need a code sample for a language not listed here, contact our support team.
