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();
}
}
``
This is my sever code