rikulo / socket.io-client-dart

socket.io-client-dart: Dartlang port of socket.io-client https://github.com/socketio/socket.io-client
https://quire.io
MIT License
675 stars 184 forks source link

Not able to access events in the custom rooms #362

Open sadhan46 opened 9 months ago

sadhan46 commented 9 months ago

This is my sever code


socket.on('join', (data) => {
        console.log('socket join')
        console.log('USERID to join ------>>>>>>' + data)
        const userId = data;
        // Check if the user already exists in the database
        if (userId != null && !io.sockets.adapter.rooms.has(userId)) {
            console.log(userId);
            console.log('userId' + userId);
            socket.join(userId);
        } else {
            console.log(socket.id + 'tried to join ' + userId + 'but the room already exist.');
            // Socket.join is not executed, hence the room not created.
        };
        console.log(io.sockets.adapter.rooms);
    });```

////.  This is where i'm trying to emit an event to a room created which is basically a userID

`
socket.on('roomTest', (data) => {
        console.log('RoomTest join')
        console.log('ROOOOM Test to join ------>>>>>>' + data)
        io.to('65bf75e048180ac736b89aab').emit('room', { "room_test": "DONE TEST" });
    });`

////.  On my app side in flutter this is the code structure 

    ``
import 'package:after/utils/secureDb.utils.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:socket_io_client/socket_io_client.dart' as io;

final socketProvider = Provider((ref) => SocketService());

class SocketService {
  late io.Socket socket;

  SocketService() {
    initSocket();
  }

  void initSocket() {
    print('socket initialized');
    socket = io.io('http://localhost:4000', <String, dynamic>{
      'transports': ['websocket'],
      'autoConnect': false,
    });

    socket.connect();

    socket.onConnect((_) async {
      print('Connected');
    });

    socket.on('65bf75e048180ac736b89aab', (data) {
      print('New Message: $data');
      // Handle new message received from the server
    });

  void disposeSocket() {
    socket.dispose();
  }
}

``
jumperchen commented 9 months ago

Which client and server version are you using?

sadhan46 commented 9 months ago

In flutter : socket_io_client: ^2.0.3+1

in node js : "socket.io": "^4.7.2"

jumperchen commented 8 months ago

Could you please try this v3.0.0-beta.0, and see if it solves your problem?