Skip to content

This project provides a simple TypeScript client for connecting to a Socket.io server. The client supports authentication and room management functionality.

License

Notifications You must be signed in to change notification settings

bunsalcoder/socket-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Socket Client

This project provides a simple TypeScript client for connecting to a Socket.io server. The client supports authentication and room management functionality.

Features

  • Connects to a Socket.io server with authentication.
  • Handles connection errors gracefully.
  • Supports joining rooms.

Installation

Ensure you have Node.js installed, then install the required dependencies:

npm install socket.io-client

Usage

Import the Client

import socketClient from './socketClient';

Initialize the Client

const socket = socketClient('https://your-socket-server.com', {
  appId: 'your-app-id',
  appSecretKey: 'your-secret-key'
});

Join a Room

socket.joinRoom && socket.joinRoom('room-name');

Listen for Room Join Event

socket.onJoinRoom && socket.onJoinRoom();

API Reference

socketClient(host: string, props: SocketClientProps): Client

Parameters:

  • 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.

Returns:

  • A Socket client instance with additional methods:
    • joinRoom(room: string): Joins a specified room.
    • onJoinRoom(): Listens for room join confirmation.

Error Handling

The client listens for connection errors and logs them to the console.

socket.on('connect_error', (error) => {
  console.log('[ERROR] Socket Client -', error.message);
});

License

MIT License

About

This project provides a simple TypeScript client for connecting to a Socket.io server. The client supports authentication and room management functionality.

Resources

License

Stars

Watchers

Forks