This project provides a simple TypeScript client for connecting to a Socket.io server. The client supports authentication and room management functionality.
- Connects to a Socket.io server with authentication.
- Handles connection errors gracefully.
- Supports joining rooms.
Ensure you have Node.js installed, then install the required dependencies:
npm install socket.io-client
import socketClient from './socketClient';
const socket = socketClient('https://your-socket-server.com', {
appId: 'your-app-id',
appSecretKey: 'your-secret-key'
});
socket.joinRoom && socket.joinRoom('room-name');
socket.onJoinRoom && socket.onJoinRoom();
host
(string) – The Socket.io server URL.props
(SocketClientProps) – An object containing:appId
(string) – Application ID for authentication.appSecretKey
(string) – Secret key for authentication.path
(string, optional) – Custom Socket.io path.
- A
Socket
client instance with additional methods:joinRoom(room: string)
: Joins a specified room.onJoinRoom()
: Listens for room join confirmation.
The client listens for connection errors and logs them to the console.
socket.on('connect_error', (error) => {
console.log('[ERROR] Socket Client -', error.message);
});
MIT License