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:
options
(Object): Configuration options for the connection.url
(string): The WebSocket URL of the Live Services endpoint.apiKey
(string, optional): Your API key for authentication.reconnect
(boolean, optional): Whether to automatically attempt reconnection on disconnect. Defaults totrue
.reconnectInterval
(number, optional): The delay in milliseconds before attempting a reconnection. Defaults to 5000.
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:
channel
(string): The name of the channel to subscribe to (e.g., 'user_updates', 'product_feed').callback
(function): A function to be called with the received data whenever a message is published to the channel.
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:
channel
(string): The name of the channel to publish to.payload
(any): The data to be sent. Should be JSON-serializable.
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:
'open'
: Fired when the connection is successfully established.'message'
: Fired when a message is received from the server. The handler receives the message data.'error'
: Fired when an error occurs. The handler receives the error object.'close'
: Fired when the connection is closed. The handler receives the close event object.'reconnect'
: Fired when the client starts attempting to reconnect.