UNPKG

e2ee-chat

Version:

A simple realtime chat SDK for web and mobile apps using socket.io with support for end-to-end encryption and multi-tenant backend integration.

129 lines (89 loc) โ€ข 2.94 kB
# ๐Ÿ’ฌ Realtime Chat SDK (Node.js / React) This SDK allows you to easily integrate **end-to-end encrypted 1-to-1 realtime chat** into your app using our hosted microservice. --- ## ๐Ÿš€ Features - ๐Ÿ”’ End-to-end AES encrypted messages - โšก๏ธ Socket-based realtime messaging - ๐Ÿงฉ Multi-tenant DB support via `apiKey` - ๐Ÿ‘ฅ Role-based user handling (client, handler, admin) - ๐Ÿ“ฅ Chat history via REST API - ๐Ÿ”„ Dynamic handler takeover logic - โœ… Works with Node.js, React.js, Next.js (App Router compatible) --- ## ๐Ÿ“ฆ Installation ```bash npm install e2ee-chat ``` --- ## ๐Ÿงช Quick Start (React or Next.js) ```jsx "use client"; import React, { useState } from "react"; import useChat from "e2ee-chat/useChat"; export default function ChatBox() { const [input, setInput] = useState(""); const { messages, sendMessage, joined } = useChat({ serverUrl: "http://localhost:4000", roomId: "session_abc123", userId: "user1", userType: "client", secretKey: "shared-secret-123", apiKey: "your-api-key-here", }); return ( <div> <h2>Chat</h2> <ul> {messages.map((m, i) => ( <li key={i}> <b>{m.senderId}</b>: {m.decryptedText} </li> ))} </ul> <input value={input} onChange={(e) => setInput(e.target.value)} /> <button onClick={() => sendMessage(input, "user2")}>Send</button> </div> ); } ``` --- ## ๐Ÿงฉ useChat Options | Option | Type | Required | Description | | ----------- | ------ | -------- | ---------------------------------- | | `serverUrl` | string | โœ… | Your hosted chat server URL | | `roomId` | string | โœ… | Session ID for the chat | | `userId` | string | โœ… | Your current user's ID | | `userType` | string | โœ… | `client`, `handler`, or `admin` | | `secretKey` | string | โœ… | AES secret used to encrypt/decrypt | | `apiKey` | string | โœ… | API key to identify your org DB | --- ## ๐Ÿ“ฅ Accessing Chat History Chat history is fetched **automatically** when the user joins. All messages are decrypted with your `secretKey`. --- ## ๐Ÿ” Admin Takeover Logic You can trigger handler takeover: ```js const { takeover } = useChat(...); takeover(); // Makes current user the new handler ``` --- ## ๐Ÿ— Works in: - โœ… React.js - โœ… Next.js App Router - โœ… Node.js test scripts --- ## ๐Ÿ“ Server Setup (Hosted Microservice) To host your own server, clone [chat-server](https://github.com/your-org/chat-server) and set up `.env`: ```env PORT=4000 MONGO_URI=your-default-db-uri ``` --- ## ๐Ÿง  Multi-Tenant Support Register your `apiKey` and `mongoUri` in the `Tenant` collection to isolate DB access per organization. --- ## ๐Ÿ“ฎ Support For issues, contact: [support@yourchatservice.com](mailto:support@yourchatservice.com) --- ## โš–๏ธ License MIT