NChat

pub package

A pure Dart/Flutter chat SDK for NBase platform with real-time messaging, GraphQL API, and Socket.IO support.

Features

  • Pure Dart Implementation: 100% Dart/Flutter with no native dependencies
  • Real-time Messaging: Socket.IO integration for instant communication
  • GraphQL API: Type-safe API calls with graphql_flutter
  • Multi-region Support: kr, beta, dev, alpha, local environments
  • Cross-platform: Works on iOS, Android, Web, Desktop
  • Android SDK Compatible: Same API interface as NBase-SDK-Android NChat

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  nchat: ^0.0.1

주요 기능

초기화 및 연결

  • initialize() - SDK 초기화
  • connect() - 사용자 로그인 및 Socket.IO 연결
  • disconnect() - 연결 해제

채널 관리

  • createChannel() - 채널 생성
  • updateChannel() - 채널 업데이트
  • deleteChannel() - 채널 삭제
  • getChannel() - 단일 채널 조회
  • getChannels() - 채널 목록 조회
  • subscribe() - 채널 구독
  • unsubscribe() - 채널 구독 해제

메시지 관리

  • sendMessage() - 텍스트 메시지 전송
  • sendFile() - 파일 업로드 및 전송
  • deleteMessage() - 메시지 삭제
  • getMessage() - 단일 메시지 조회
  • getMessages() - 메시지 목록 조회
  • unreadCount() - 안읽은 메시지 수 조회

멤버 관리

  • addUsers() - 채널에 사용자 추가
  • removeUsers() - 채널에서 사용자 제거
  • banUser() - 사용자 차단
  • unbanUser() - 사용자 차단 해제

Pin 관리

  • createPin() - 메시지 핀 생성
  • updatePin() - 핀 업데이트
  • getPin() - 단일 핀 조회
  • getPins() - 핀 목록 조회

푸시 알림

  • setPushToken() - 푸시 토큰 설정
  • setEnablePush() - 푸시 활성화/비활성화
  • setPushState() - 채널별 푸시 설정
  • getPushState() - 채널별 푸시 상태 조회

이벤트 핸들링

  • addHandler() - 이벤트 핸들러 등록
  • removeHandler() - 이벤트 핸들러 제거

Quick Start

import 'package:nchat/nchat.dart';
import 'package:flutter/material.dart';

class ChatScreen extends StatefulWidget {
  @override
  State<ChatScreen> createState() => _ChatScreenState();
}

class _ChatScreenState extends State<ChatScreen> implements ChatHandler {
  final _nchat = NChat();

  @override
  void initState() {
    super.initState();
    _nchat.addHandler('main', this);
    _initializeChat();
  }

  Future<void> _initializeChat() async {
    // 1. Initialize SDK
    await _nchat.initialize(
      projectId: 'your-project-id',
      region: 'kr',
    );

    // 2. Connect with user
    final user = await _nchat.connect(
      userId: 'user-001',
      name: 'John Doe',
    );

    // 3. Create a channel
    final channel = await _nchat.createChannel(
      name: 'General',
      type: 'public',
    );

    // 4. Send a message
    await _nchat.sendMessage(
      channelId: channel.id,
      content: 'Hello, World!',
    );
  }

  // ChatHandler implementation
  @override
  void onMessageReceived(Message message) {
    print('New message: ${message.content}');
    setState(() {
      // Update UI
    });
  }

  @override
  void onConnected() {
    print('Connected to server');
  }

  @override
  void onDisconnected(String reason) {
    print('Disconnected: $reason');
  }

  @override
  void dispose() {
    _nchat.removeHandler('main');
    super.dispose();
  }
}

API Overview

Initialization & Connection

  • initialize() - Initialize SDK
  • connect() - User login and Socket.IO connection
  • disconnect() - Disconnect from server

Channel Management

  • createChannel() - Create a new channel
  • updateChannel() - Update channel information
  • deleteChannel() - Delete a channel
  • getChannel() - Get single channel
  • getChannels() - Get channel list
  • subscribe() - Subscribe to a channel
  • unsubscribe() - Unsubscribe from a channel

Message Operations

  • sendMessage() - Send text message
  • sendFile() - Upload and send file
  • deleteMessage() - Delete a message
  • getMessage() - Get single message
  • getMessages() - Get message list
  • unreadCount() - Get unread message count

Member Management

  • addUsers() - Add users to channel
  • removeUsers() - Remove users from channel
  • banUser() - Ban a user
  • unbanUser() - Unban a user

Push Notifications

  • setPushToken() - Set push token
  • setEnablePush() - Enable/disable push
  • setPushState() - Set push state per channel
  • getPushState() - Get push state

Event Handling

  • addHandler() - Register event handler
  • removeHandler() - Remove event handler

Region Configuration

Region Description
kr Korea production server
beta Beta server
dev Development server (default)
alpha Alpha server
local Local server (localhost:4000)

Error Handling

try {
  await nchat.sendMessage(
    channelId: 'channel-id',
    content: 'Hello',
  );
} on ChatException catch (e) {
  print('Error code: ${e.code}');
  print('Error message: ${e.message}');
}

File Upload

final file = File('/path/to/image.jpg');

final message = await nchat.sendFile(
  channelId: 'channel-id',
  file: file,
  message: 'Image uploaded',
  onProgress: (sent, total) {
    print('Progress: ${(sent / total * 100).toStringAsFixed(1)}%');
  },
);

Example

See the example directory for a complete sample app.

License

Proprietary License - see the LICENSE file for details.

This software is proprietary to NBase and may only be used in connection with NBase services.

Libraries

nchat