패키지, 저장소 및 앱 카테고리 검색

패키지 상세v0.6.0

flutter_socket_io

Flutter Socket IO 플러그인

30일 다운로드
158
좋아요
66
Pub 점수
30

패키지 설치

flutter pub add flutter_socket_io

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

Flutter 앱과 Socket.IO 서버 간 실시간 양방향 통신을 가능하게 하는 플러터 플러그인입니다. Socket.IO 프로토콜과의 통합을 단순화하며, 이벤트 기반 메시징, 방 기반 브로드캐스팅 및 연결 관리를 지원합니다. 채팅 앱, 협업 도구, 실시간 대시보드와 같이 실시간 업데이트가 필요한 애플리케이션에서 원활하게 사용할 수 있도록 설계되었습니다. 다양한 플랫폼에서 작동하며, Dart 바인딩을 통해 원래의 Socket.IO 자바스크립트 라이브러리의 강력함을 활용합니다.

사용 사례

  • 실시간 채팅 애플리케이션
  • 실시간 데이터 스트리밍
  • 협업 편집 도구
  • 온라인 게임
  • 사물인터넷 장치 모니터링

주요 기능

  • 양방향 통신
  • 이벤트 기반 메시징
  • 룸 및 네임스페이스 지원
  • 자동 재연결
  • 크로스 플랫폼 호환성

적합한 대상

  • 실시간 업데이트가 필요한 앱
  • Socket.IO에 익숙한 개발자
  • 저지연 통신이 필요한 프로젝트
  • 인터랙티브 웹 경험을 구축하는 팀

고려 사항

  • 호환되는 Socket.IO 서버가 필요합니다
  • 네트워크 문제에 대한 사용자 정의 오류 처리가 필요할 수 있습니다
  • 안정적인 인터넷 연결에 의존함
  • 고빈도, 저지연 마이크로 트랜잭션에 적합하지 않음

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

flutter_socket_io

Flutter Socket IO Plugin, supported Android + iOS (iOS installation guide is coming soon)

How to install on iOS

    1. Copy the folder example/ios/Runner/SocketObj to ${PROJECT_ROOT}/ios/Runner/
    1. Replace the example/ios/Runner/AppDelegate.m line with ${PROJECT_ROOT}/ios/Runner/AppDelegate.m. (Notice: You should merge the old one in your project to merge with the new from this plugin if you have some change on that file)
    1. Open ${PROJECT_ROOT}/ios/Podfile, paste this line pod 'Socket.IO-Client-Swift', '~> 13.3.0' before the end of target 'Runner' do block
    1. Run and Enjoy the plugin :)

Use the plugin

1. Add the following import to your Dart code:

import  'package:flutter_socket_io/flutter_socket_io.dart';

2. SocketIOManager: to manage (create/destroy) list of SocketIO

Create SocketIO with SocketIOManager:

SocketIO socketIO = SocketIOManager().createSocketIO("http://127.0.0.1:3000", "/chat", query: "userId=21031", socketStatusCallback: _socketStatus);  

Destroy SocketIO with socketIOManager:

SocketIOManager().destroySocket(socketIO);  

3. SocketIO:

Get Id (Url + Namespace) of the socket

String getId();


Create a new socket and connects the client

Future<void> connect();


Init socket before doing anything with socket

Future<void> init();


Subscribe to a channel with a callback

Future<void> subscribe(String event, Function callback); 


Unsubscribe from a channel. When no callback is provided, unsubscribe all subscribers of the channel. Otherwise, unsubscribe only the callback passed in

Future<void> unSubscribe(String event, [Function callback]); 


Send a message via a channel (i.e. event, the native code will convert string message to JsonObject before sending)

Future<void> sendMessage(String event, dynamic message, [Function callback]);


Disconnect from the socket

Future<void> disconnect(); 


Unsubscribe all subscribers from all channels

Future<void> unSubscribesAll();


4. Example: Link

SocketIO socketIO;
_connectSocket01() { 
	//update your domain before using  
	 socketIO = SocketIOManager().createSocketIO("http://127.0.0.1:3000", "/chat", query: "userId=21031", socketStatusCallback: _socketStatus); 

	//call init socket before doing anything 
	socketIO.init(); 

	//subscribe event
	socketIO.subscribe("socket_info", _onSocketInfo); 

	//connect socket 
	socketIO.connect(); 
}

_socketStatus(dynamic data) { 
	print("Socket status: " + data); 
}

_subscribes() { 
	if (socketIO != null) { 
		socketIO.subscribe("chat_direct", _onReceiveChatMessage); 
	} 
}

void _onReceiveChatMessage(dynamic message) { 
	print("Message from UFO: " + message); 
}

void _sendChatMessage(String msg) async { 
	if (socketIO != null) { 
		String jsonData = '{"message":{"type":"Text","content": ${(msg != null && msg.isNotEmpty) ? '"${msg}"' : '"Hello SOCKET IO PLUGIN :))"'},"owner":"589f10b9bbcd694aa570988d","avatar":"img/avatar-default.png"},"sender":{"userId":"589f10b9bbcd694aa570988d","first":"Ha","last":"Test 2","location":{"lat":10.792273999999999,"long":106.6430356,"accuracy":38,"regionId":null,"vendor":"gps","verticalAccuracy":null},"name":"Ha Test 2"},"receivers":["587e1147744c6260e2d3a4af"],"conversationId":"589f116612aa254aa4fef79f","name":null,"isAnonymous":null}'; 
		socketIO.sendMessage("chat_direct", jsonData, _onReceiveChatMessage); 
	}
 }

_destroySocket() { 
	if (socketIO != null) { 
		SocketIOManager().destroySocket(socketIO); 
	} 
}


카테고리

소스 저장소

이 패키지를 사용하는 프로젝트