Skip to content

Instantly share code, notes, and snippets.

@MarceloRab
Created November 14, 2024 13:58
Show Gist options
  • Save MarceloRab/9100a31439219fa045f0278a2d4427ef to your computer and use it in GitHub Desktop.
Save MarceloRab/9100a31439219fa045f0278a2d4427ef to your computer and use it in GitHub Desktop.
Socket + Flutter example
// Flutte side
import 'package:flutter/material.dart';
//ignore: library_prefixes
import 'package:socket_io_client/socket_io_client.dart' as IO;
const apiSocketUrl = 'wss://<render-end-point>.com';
const apiSocketUrl = 'http://localhost:8000';
//πŸ‘‰πŸΌ obs: Android and Localhost worked perfectly.
class SocketUserClient {
late final IO.Socket socket;
SocketUserClient() {
socketUsersClient();
}
void socketUsersClient() {
socket = IO.io(apiSocketUrl, <String, dynamic>{
'autoConnect': true,
'transports': ['websocket'],
});
socket.connect();
//debugPrint('πŸš€ user_socket_client.dart; socketConnected ID -');
socket.onConnect((_) {
debugPrint('πŸš€ user_socket_client.dart; LIGADO socket CLient DART');
});
socket.on('user update', (data) => debugPrint('πŸš€ user_socket_client.dart; UPDATE - $data'));
socket.on('user removed', (data) => debugPrint('πŸš€ user_socket_client.dart; data - $data '));
socket
.onDisconnect((_) => debugPrint('πŸš€ user_socket_client.dart; Cliente Socket Desconectado'));
socket.onError((err) => debugPrint('πŸš€ user_socket_client.dart; err - $err'));
}
void disposeSocket() {
socket.disconnect();
socket.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment