@oichat/sdk
v0.1.1
Published
OiChat TypeScript SDK — Real-time chat SDK for web and React Native
Readme
@oichat/sdk
Real-time chat SDK for web and React Native — REST API client + Centrifugo WebSocket integration.
Installation
npm install @oichat/sdk
# or
yarn add @oichat/sdkQuick Start
Server Mode
Your backend issues JWT tokens and passes them to the client.
import { OiChat } from "@oichat/sdk";
const oichat = OiChat.init(
{
apiUrl: "https://api.oichatapi.com",
wsUrl: "wss://ws.oichatapi.com/connection/websocket",
projectId: "proj_xxxxxxxxxxxx",
secretKey: "sk_xxxxxxxxxxxx",
},
{ userId: "user-123" },
);
// Connect with JWT token from your backend
await oichat.realtime.connect({ token: jwtFromServer });Client Mode
SDK handles token issuance and auto-refresh internally.
import { OiChat } from "@oichat/sdk";
const oichat = await OiChat.initClient(
{
apiUrl: "https://api.oichatapi.com",
wsUrl: "wss://ws.oichatapi.com/connection/websocket",
projectId: "proj_xxxxxxxxxxxx",
publicKey: "pk_xxxxxxxxxxxx",
},
{ userId: "user-123", userName: "John Doe" },
);Core APIs
Rooms
// List rooms
const { data, next_cursor } = await oichat.rooms.list({ limit: 20 });
// Create room
const room = await oichat.rooms.create({
type: "group",
name: "Support Chat",
participant_ids: ["user-1", "user-2"],
});Messages
// Subscribe to a room
const sub = await oichat.realtime.subscribeRoom("room-id");
// Listen for new messages
sub.onMessage((message) => {
console.log(message.content);
});
// Send a message
await oichat.messages.send({
room_id: "room-id",
content: "Hello!",
content_type: "text",
});Real-time Events
// Typing indicator
sub.onTyping((event) => {
console.log(`${event.user_id} is typing...`);
});
// Read receipts
sub.onRead((event) => {
console.log(`${event.user_id} read messages`);
});
// Connection state
oichat.realtime.onConnectionStateChange.on((state) => {
console.log("Connection:", state); // 'connecting' | 'connected' | 'disconnected'
});Push Notifications
// Register FCM token
await oichat.push.registerToken({
token: fcmToken,
device_type: "web",
});File Upload
// Upload with progress
const result = await oichat.messages.sendWithAttachment({
room_id: "room-id",
file,
onProgress: (percent) => console.log(`${percent}%`),
});Cleanup
// Disconnect and release resources
oichat.dispose();Links
- Documentation — Full guides and API reference
- Sample: React + Next.js — Complete working example
- UI Components (@oichat/ui-react) — Pre-built chat widgets
- Server SDK (@oichat/server-sdk) — Backend token issuance & room management
한국어
모바일 앱과 웹에 채팅을 붙이는 가장 쉬운 방법 — REST API 클라이언트 + Centrifugo WebSocket 실시간 통신.
설치
npm install @oichat/sdk
# 또는
yarn add @oichat/sdk빠른 시작
서버 모드
백엔드에서 JWT 토큰을 발급하여 클라이언트에 전달하는 방식입니다.
import { OiChat } from "@oichat/sdk";
const oichat = OiChat.init(
{
apiUrl: "https://api.oichatapi.com",
wsUrl: "wss://ws.oichatapi.com/connection/websocket",
projectId: "proj_xxxxxxxxxxxx",
secretKey: "sk_xxxxxxxxxxxx",
},
{ userId: "user-123" },
);
// 백엔드에서 받은 JWT 토큰으로 연결
await oichat.realtime.connect({ token: jwtFromServer });클라이언트 모드
SDK가 자동으로 토큰 발급/갱신 + 연결을 처리합니다.
import { OiChat } from "@oichat/sdk";
const oichat = await OiChat.initClient(
{
apiUrl: "https://api.oichatapi.com",
wsUrl: "wss://ws.oichatapi.com/connection/websocket",
projectId: "proj_xxxxxxxxxxxx",
publicKey: "pk_xxxxxxxxxxxx",
},
{ userId: "user-123", userName: "홍길동" },
);주요 기능
| 기능 | 설명 | | ------------- | ------------------------- | | 채팅방 관리 | 생성, 목록, 참여, 나가기 | | 실시간 메시징 | Centrifugo WebSocket 기반 | | 프레전스 | 온라인/오프라인 상태 추적 | | 파일 업로드 | 이미지, 파일 전송 지원 | | 푸시 알림 | FCM 토큰 등록 | | 멀티 테넌트 | 프로젝트 단위 데이터 격리 |
관련 링크
- 공식 문서 — 가이드 및 API 레퍼런스
- 샘플: React + Next.js — 전체 동작 예제
- UI 컴포넌트 (@oichat/ui-react) — 즉시 사용 가능한 채팅 위젯
- 서버 SDK (@oichat/server-sdk) — 백엔드 토큰 발급 및 방 관리
License
MIT
