The MatchmakeClient library provides an easy way to connect to the Matchmake.io service in both browser and Node.js environments.
# Install the client librarynpm install matchmake-client# For Node.js environments, also install WebSocket supportnpm install ws
import { MatchmakeClient } from 'matchmake-client';// Create a client instanceconst client = new MatchmakeClient('wss://api.matchmake.io/socket', {apiToken: 'your-api-token', // Optional: defaults to a demo tokendebug: true // Optional: enables logging});// Set up event listenersclient.on('connected', (configs) => {console.log('Connected with configurations:', configs);});client.on('lobbyUpdate', (lobby) => {console.log('Lobby updated:', lobby);});
Connect to the matchmaking service and authenticate a player:
// Connect to the matchmaking serviceasync function connect() {try {const configs = await client.connect({playerSlug: 'player123', // Player identifieripAddress: '127.0.0.1' // Player's IP address});console.log('Available matchmaking configurations:', configs);// Connection successful, now you can find and join lobbies} catch (error) {console.error('Connection failed:', error.message);}}
The connect method returns a promise that resolves to an array of available matchmaking configurations.
The client emits various events you can listen for:
// Basic connection eventsclient.on('connected', (configs) => {console.log('Connected to matchmaking service');});client.on('disconnected', () => {console.log('Disconnected from matchmaking service');});client.on('error', (error) => {console.error('An error occurred:', error.message);});client.on('socketError', (error) => {console.error('Socket error:', error.message);});client.on('socketClosed', () => {console.log('WebSocket connection closed');});
Find, join, and interact with lobbies:
// Find a lobby for a specific configurationasync function findLobby(configId) {try {const lobby = await client.findLobby(configId);console.log('Found lobby:', lobby);return lobby;} catch (error) {console.error('Failed to find lobby:', error.message);}}
// Join a specific lobbyasync function joinLobby(lobby) {try {const joinedLobby = await client.joinLobby(lobby);console.log('Joined lobby:', joinedLobby);return joinedLobby;} catch (error) {console.error('Failed to join lobby:', error.message);}}// Or use the convenience method to find and join in one stepasync function findAndJoinLobby(configId) {try {const lobby = await client.findAndJoinLobby(configId);console.log('Found and joined lobby:', lobby);return lobby;} catch (error) {console.error('Failed to find and join lobby:', error.message);}}
// Set host information for the current lobbyasync function setHostInfo() {try {await client.setHostInfo({type: 'p2p', // 'p2p' or 'dedicated'address: '192.168.1.100', // Host addressport: 7777 // Host port});console.log('Host information set successfully');} catch (error) {console.error('Failed to set host information:', error.message);}}
// Leave the current lobbyasync function leaveLobby() {try {await client.leaveLobby();console.log('Left lobby successfully');} catch (error) {console.error('Failed to leave lobby:', error.message);}}// Disconnect from the matchmaking service completelyfunction disconnect() {client.disconnect();console.log('Disconnected from matchmaking service');}
Listen for lobby-specific events:
// Lobby eventsclient.on('lobbyFound', (lobby) => {console.log('Lobby found:', lobby);});client.on('lobbyJoined', (lobby) => {console.log('Joined lobby:', lobby);});client.on('lobbyUpdate', (lobby) => {console.log('Lobby updated:', lobby);});client.on('lobbyLeft', (lobbyId) => {console.log('Left lobby:', lobbyId);});client.on('playerJoined', (player, lobby) => {console.log('Player joined:', player.slug);});client.on('playerLeft', (player, lobby) => {console.log('Player left:', player.slug);});client.on('statusChanged', (status, lobby) => {console.log('Lobby status changed to:', status);});client.on('hostChanged', (hostInfo, lobby) => {console.log('Host information changed:', hostInfo);});client.on('hostInfoSet', (hostInfo) => {console.log('Successfully set host info:', hostInfo);});
Use auto-matching to let the system find opponents:
// Using auto-matching to find playersasync function startAutoMatch(configId) {try {// Connect to the matchmaking serviceawait client.connect({playerSlug: 'player123',ipAddress: '127.0.0.1'});// Find and join a lobby with auto-matching// (Make sure the configuration has lobby_type: 'auto_match')const lobby = await client.findAndJoinLobby(configId);console.log('Joined auto-match lobby:', lobby);// Listen for lobby updatesclient.on('statusChanged', (status, updatedLobby) => {if (status === 'full' || status === 'started') {console.log('Match is ready to start!');console.log('Players:', updatedLobby.players);if (updatedLobby.host_type && updatedLobby.host_address) {console.log('Host info:', {type: updatedLobby.host_type,address: updatedLobby.host_address,port: updatedLobby.host_port});}}});// Listen for player joinsclient.on('playerJoined', (player, updatedLobby) => {console.log('New player joined:', player.slug);console.log('Current player count:', updatedLobby.players.length);});} catch (error) {console.error('Auto-match failed:', error.message);}}
For auto-matching, use a configuration that has lobby_type: 'auto_match'.
The client provides specific error classes to help with error handling:
import {MatchmakeError, // Base error classConnectionError, // WebSocket or connection issuesLobbyError // Lobby-related errors} from 'matchmake-client';// Example error handlingtry {await client.findAndJoinLobby(configId);} catch (error) {if (error instanceof ConnectionError) {console.error('Connection issue:', error.message);// Handle connection problems} else if (error instanceof LobbyError) {console.error('Lobby issue:', error.message);// Handle lobby-specific problems} else {console.error('Unexpected error:', error.message);// Handle other errors}}
import { MatchmakeClient, ConnectionError, LobbyError } from 'matchmake-client';async function startGame() {// Create clientconst client = new MatchmakeClient('wss://api.matchmake.io/socket', {apiToken: 'your-api-token',debug: true});// Set up event listenersclient.on('connected', (configs) => {console.log('Connected with configs:', configs);});client.on('lobbyJoined', (lobby) => {console.log('Successfully joined lobby:', lobby.id);});client.on('playerJoined', (player) => {console.log('Player joined:', player.slug);});client.on('statusChanged', (status, lobby) => {if (status === 'started') {console.log('Game is starting!');// Connect to the game hostconnectToGame(lobby.host_address, lobby.host_port);}});try {// Connect to matchmaking serviceawait client.connect({playerSlug: 'player123',ipAddress: '127.0.0.1'});// Find and join a lobbyconst lobby = await client.findAndJoinLobby(1);console.log('Joined lobby:', lobby);// If you're hosting, set host infoif (isHosting) {await client.setHostInfo({type: 'p2p',address: getMyIpAddress(),port: 7777});}} catch (error) {if (error instanceof ConnectionError) {console.error('Connection failed:', error.message);} else if (error instanceof LobbyError) {console.error('Lobby error:', error.message);} else {console.error('Unexpected error:', error.message);}}}// Start the matchmaking processstartGame();// Cleanup when donefunction cleanup() {client.leaveLobby().then(() => client.disconnect()).catch(error => console.error('Cleanup error:', error));}