> For the complete documentation index, see [llms.txt](https://docs.thesports.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.thesports.com/reference/sample-code.md).

# Sample code

## Sample code

This compact `paho-mqtt` pattern connects over TLS WebSocket transport. Keep credentials and topics outside source control.

```python
# python example

import json

import paho.mqtt.client as mqtt

# Football Topic
football_topic = ''
# Basketball Topic
basketball_topic = ''
# Tennis Topic
tennis_topic = ''

# username
username = ''
# password
password = ''


# Connection callback
def on_connect(c, userdata, flags, rc):
    # The value of rc indicates success or not:
    # 0: Connection successful
    # 1: Connection refused - incorrect protocol version
    # 2: Connection refused - invalid client identifier
    # 3: Connection refused - server unavailable
    # 4: Connection refused - bad username or password
    # 5: Connection refused - not authorised
    # 6-255: Currently unused.

    # connection succeeded
    if rc == 0:
        # Subscribe to related topics
        c.subscribe(football_topic)
        c.subscribe(basketball_topic)
        c.subscribe(tennis_topic)
    elif rc in [4, 5]:
        print('If the verification fails, please confirm whether the user name, key, and authorized ip are correct, otherwise the authentication will fail')


# Message callback
def on_message(c, userdata, msg):
    # Message processing logic, please refer to the document for specific format
    print(msg.topic)
    print(json.loads(msg.payload))


if __name__ == '__main__':
    # websocket protocol
    client = mqtt.Client(transport='websockets')
    client.tls_set()
    client.username_pw_set(username=username, password=password)
    client.on_connect = on_connect
    client.on_message = on_message

    client.connect("mq.thesports.com", 443)
    client.loop_forever()
```

Use account-provided topics only. Add durable storage, message validation, and observability before production use.

### Next steps

Read [WebSocket via MQTT](/integration/websocket-via-mqtt.md) for access and reconnection behavior. Follow [Updates and synchronization](/integration/updates-and-synchronization.md) for local state handling.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.thesports.com/reference/sample-code.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
