Real-Time Client - livef1.adapters.realtime_client#

Classes

RealF1Client(topics[, log_file_name, ...])

A client for managing real-time Formula 1 data streaming.

MessageHandlerTemplate(func)

A template for handling incoming live timing messages.

class livef1.adapters.realtime_client.MessageHandlerTemplate(func)[source]#

A template for handling incoming live timing messages.

Incoming topic data is parsed with function_map and the resulting records are passed to a user-defined function.

Parameters:
funccallable

A user-defined asynchronous function that processes the parsed records from incoming messages. The function must accept the processed records as its input.

Methods

get(topic, data, timestamp)

Parse the data of a topic and invoke the handler function with {topic: records}.

async get(topic, data, timestamp)[source]#

Parse the data of a topic and invoke the handler function with {topic: records}.

Parameters:
topicstr

The topic name, e.g. "CarData.z".

datadict or str

The topic data.

timestampstr or None

The message timestamp; None for the initial snapshot.

class livef1.adapters.realtime_client.RealF1Client(topics, log_file_name=None, log_file_mode='w')[source]#

A client for managing real-time Formula 1 data streaming.

The client connects to the live timing SignalR Core hub with the F1TV token from livef1.login(), signing in first if needed.

Parameters:
topicsstr or list

Topic(s) to subscribe to for live updates.

log_file_namestr, optional

Name of the log file (default is None).

log_file_modestr, optional

Mode for opening the log file (default is “w”).

Attributes:
topicslist

List of topics to subscribe to for receiving live data.

headersdict

HTTP headers used for the connection.

_connection_urlstr

URL for the SignalR connection.

_log_file_namestr

Path to the log file.

_log_file_modestr

Mode for opening the log file.

_log_filefile object

Log file object for writing messages.

_handlersdict

Mapping of methods to their respective handlers.

Methods

callback(method)

Decorator to register a callback function for a specific method.

on_message(method, handler)

Register a handler for a specific method.

run()

Start the client and block until it is interrupted.

callback(method)[source]#

Decorator to register a callback function for a specific method.

This decorator allows you to associate a callback function with a particular method. The function being registered must have arguments matching the required parameters defined in REALTIME_CALLBACK_DEFAULT_PARAMETERS.

Raises:
TypeError

If the provided callback function does not have the required arguments.

Notes

  • The REALTIME_CALLBACK_DEFAULT_PARAMETERS is a predefined list of parameter names that the callback function must include.

  • Once the function is successfully registered, it will handle messages for the specified method.

Examples

Registering a callback for a method:

from livef1.adapters.realtime_client import RealF1Client

client = RealF1Client(
    topics = ["CarData.z", "SessionInfo"],
    log_file_name="./output.json"
)

@client.callback("new one")
async def print_callback(records): # records argument have to be set
    print(records) # or you can do whatever you want with incoming data

client.run()
on_message(method, handler)[source]#

Register a handler for a specific method.

Parameters:
methodstr

The method to handle.

handlercallable

The function to handle the method.

run()[source]#

Start the client and block until it is interrupted.

Warning

The RealF1Client streams data from external live timing services. These services are not controlled by this library, so topic names, availability, and formats may change without notice. Use the real-time client responsibly and verify your use case against any applicable data usage terms.