Live Services API

Documentation for the Live Services API, designed to enable real-time data synchronization and interaction.

Service Connection

connect(options)

Establishes a connection to the Live Services endpoint.

Promise<Connection> connect(options: ConnectionOptions)

Parameters:

Returns:

A Promise that resolves with a Connection object upon successful connection.

Example:


import { connect } from '@msdn/api/live';

async function initializeLiveService() {
    try {
        const connection = await connect({
            url: 'wss://api.msdn.live.com/v1',
            apiKey: 'YOUR_API_KEY_HERE',
            reconnect: true
        });
        console.log('Successfully connected to Live Services.');

        connection.on('message', (data) => {
            console.log('Received message:', data);
        });

        connection.on('disconnect', () => {
            console.warn('Disconnected from Live Services.');
        });

    } catch (error) {
        console.error('Failed to connect to Live Services:', error);
    }
}

initializeLiveService();
                

Real-time Data Synchronization

subscribe(channel, callback)

Subscribes to a specific channel to receive real-time updates.

void subscribe(channel: string, callback: (data: any) => void)

Parameters:

Note: Ensure you are connected to the Live Services before subscribing.

Example:


// Assuming 'connection' is an established Connection object from connect()
connection.subscribe('user_presence', (userData) => {
    console.log(`User presence update for ${userData.userId}: ${userData.status}`);
});
                

publish(channel, payload)

Publishes a message to a specified channel.

void publish(channel: string, payload: any)

Parameters:

Note: Publishing may require specific permissions depending on the channel.

Example:


// Assuming 'connection' is an established Connection object from connect()
const statusUpdate = {
    userId: 'user123',
    status: 'online',
    lastSeen: new Date().toISOString()
};
connection.publish('user_presence', statusUpdate);
                

Connection Management

The Connection object provides methods for managing the live service connection.

disconnect()

Gracefully closes the connection to the Live Services.

void disconnect()

on(event, handler)

Registers an event listener for connection events.

void on(event: 'open' | 'message' | 'error' | 'close' | 'reconnect', handler: (...args: any[]) => void)

Supported Events: