
# WA Master - WhatsApp Multi-Client Manager

WA Master is a scalable, efficient, and fault-tolerant multi-client management solution for WhatsApp built on Node.js using the Baileys library. This system leverages Redis for data storage and caching, providing robust features like event handling, worker threads, and LRU caching.

## Features

- **Multi-client Management**: Manage multiple WhatsApp clients simultaneously.
- **Worker Thread Architecture**: Each client runs in its dedicated worker thread.
- **Redis Integration**: Store and retrieve chats, messages, and other data efficiently using Redis.
- **Scalable and Resilient**: Supports reconnections, error handling, and maximum restart attempts.
- **Event-driven System**: Process and respond to various WhatsApp events like messages, presence updates, and connections.
- **TypeScript Support**: Strong type safety for all components and handlers.

## Table of Contents

- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Configuration Options](#configuration-options)
- [API Reference](#api-reference)
- [Contributing](#contributing)
- [License](#license)

## Installation

### Prerequisites

- Node.js (>= 16.x)
- Redis server (>= 6.x)

### Steps

1. Clone this repository:

```bash
git clone https://github.com/your-username/wa-master.git
cd wa-master
```

2. Install dependencies:

```bash
npm install
```

3. Start Redis server:

```bash
redis-server
```

## Usage

### Starting the Application

To start managing WhatsApp clients:

```bash
npm start
```

### Sample Code

Below is an example to demonstrate adding and managing a WhatsApp client:

```typescript
import { WAMaster } from './src/index';
import { WAClientConfig } from './src/client/types';

const master = new WAMaster();

const clientConfig: WAClientConfig = {
  clientId: 'unique-client-id',
  redisStoreUrl: 'redis://localhost:6379',
  browser: ['Chrome', '91', 'MacOS'],
};

master.startClient(clientConfig);

master.on('messages.upsert', (data) => {
  console.log('New messages:', data);
});
```

## Configuration Options

### RedisStorage

The `RedisStorage` class handles data operations using Redis. It provides methods to register, fetch, and clean up data.

Key Options:

| Option          | Type          | Default    | Description                                    |
|------------------|---------------|------------|------------------------------------------------|
| `sessionId`     | `string`      | Required   | Unique identifier for the client session.     |
| `redis`         | `RedisClient` | Required   | Instance of Redis client for storage.         |
| `logger`        | `Logger`      | `null`     | Optional logger for debugging.                |

### WAClientConfig

| Option                 | Type          | Default    | Description                                |
|-------------------------|---------------|------------|--------------------------------------------|
| `clientId`             | `string`      | Required   | Unique client identifier.                  |
| `redisStoreUrl`        | `string`      | Required   | URL of the Redis instance.                 |
| `browser`              | `string[]`    | Required   | Browser information for WhatsApp Web.      |
| `shouldSyncHistoryMessage` | `boolean` | `false`    | Whether to sync history messages.          |

## API Reference

### WAMaster Class

#### `startClient(clientConfig: WAClientConfig): void`
Starts a new WhatsApp client with the given configuration.

#### `stopClient(clientId: string): void`
Stops a running WhatsApp client.

#### `on<T>(event: T, callback: Function): void`
Registers a callback for a specific event.

#### `off<T>(event: T, callback: Function): void`
Removes a callback for a specific event.

## Contributing

Contributions are welcome! To contribute:

1. Fork the repository.
2. Create a feature branch: `git checkout -b feature/your-feature`.
3. Commit your changes: `git commit -m 'Add new feature'`.
4. Push the branch: `git push origin feature/your-feature`.
5. Submit a pull request.

## License

This project is licensed under the MIT License. See the LICENSE file for details.

---

For more information, visit the [GitHub Repository](https://github.com/your-username/wa-master).
