# Code Examples

## JavaScript / Node.js

```javascript
const io = require("socket.io-client");

const SERVER_URL = "wss://rms.lsports.eu";
const CUSTOMER_ID = "YOUR_CUSTOMER_ID";
const ACCESS_TOKEN = "YOUR_ACCESS_TOKEN";

const socket = io(SERVER_URL, {
  path: "/rms-socket/socket.io",
  transports: ["websocket", "polling"],
  extraHeaders: {
    Authorization: `Bearer ${ACCESS_TOKEN}`,
    customerid: CUSTOMER_ID,
  },
  reconnection: true,
  reconnectionAttempts: 10,
  reconnectionDelay: 1000,
});

// Connection events
socket.on("connect", () => {
  console.log("Connected:", socket.id);
  socket.emit("join", {}, (response) => {
    console.log("Room:", response.room);
  });
});

socket.on("connect_error", (error) => {
  console.error("Connection failed:", error.message);
});

socket.on("disconnect", (reason) => {
  console.log("Disconnected:", reason);
});

socket.on("reconnect", () => {
  console.log("Reconnected");
  socket.emit("join", {});
});

// Business events
socket.on("RESERVE_BET_ACCEPT", (data) => {
  console.log("Bet accepted:", data);
});

socket.on("RESERVE_BET_REJECT", (data) => {
  console.log("Bet rejected:", data);
});

socket.on("announcements", (data) => {
  console.log("Announcement:", data.message);
});
```

***

## Python

```python
import socketio

SERVER_URL = "wss://rms.lsports.eu"
CUSTOMER_ID = "YOUR_CUSTOMER_ID"
ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"

sio = socketio.Client()

@sio.event
def connect():
    print("Connected!")
    sio.emit("join", {}, callback=on_join)

def on_join(response):
    print(f"Joined room: {response['room']}")

@sio.event
def connect_error(error):
    print(f"Connection failed: {error}")

@sio.event
def disconnect():
    print("Disconnected")

@sio.on("RESERVE_BET_ACCEPT")
def on_bet_accept(data):
    print(f"Bet accepted: {data}")

@sio.on("RESERVE_BET_REJECT")
def on_bet_reject(data):
    print(f"Bet rejected: {data}")

sio.connect(
    SERVER_URL,
    socketio_path="/rms-socket/socket.io",
    transports=["websocket", "polling"],
    headers={
        "Authorization": f"Bearer {ACCESS_TOKEN}",
        "customerid": CUSTOMER_ID,
    }
)

sio.wait()
```

***

## Browser

```html
<script src="https://cdn.socket.io/4.0.0/socket.io.min.js"></script>
<script>
const socket = io("wss://rms.lsports.eu", {
  path: "/rms-socket/socket.io",
  transports: ["websocket", "polling"],
  extraHeaders: {
    Authorization: "Bearer YOUR_ACCESS_TOKEN",
    customerid: "YOUR_CUSTOMER_ID",
  },
});

socket.on("connect", () => {
  console.log("Connected:", socket.id);
  socket.emit("join", {}, (response) => {
    console.log("Room:", response.room);
  });
});

socket.on("RESERVE_BET_ACCEPT", (data) => {
  console.log("Bet accepted:", data);
  // Update your UI here
});

socket.on("connect_error", (error) => {
  console.error("Error:", error.message);
});
</script>
```

***

## Related

* [Quick Start & Best Practices](/u/defend/websocket-connection/quick-start-and-best-practices.md)
* [Error Handling](/u/defend/websocket-connection/error-handling.md)
* [Authentication Guide](/u/defend/integration-guide/authentication.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.lsports.eu/u/defend/websocket-connection/code-examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
