I am facing an issue connecting my socket io flutter client to my nodejs socket io server. I am aware of the compatibility issue between the node socket io package and the flutter socket io client package. Hence, I installed only compatible versions of dependencies on both ends.
i.e., for flutter
socket_io_client: ^1.0.2
and for server side.
{
"socket.io": "^2.4.1",
}
I have also tried the latest dependencies but I am getting the same timeout message in my logs, Initially, I thought it was my server's issue, but I checked the connection using postman and it works.
Below I am adding my implementations for both server and client side,
const { socketGuard } = require('./middlewares/socketio.middleware');
let io;
module.exports = {
init: (server) => {
io = require("socket.io")(server, { cors: { origin: '*' } });
io.use(async (socket, next) => {
let result = await socketGuard(socket);
if (result.isValid) {
next();
}
else {
next(new Error("Not authorized"));
}
});
io.on("connection", (socket) => {
console.log(`${socket.id} : connected`);
});
return io;
},
get: () => {
if (!io) {
throw new Error("Socket io not initialized");
}
return io;
}
};
and for flutter, I have added the following lines in the init function,
io.Socket socket = io.io("http://192.168.1.14:5000");
socket.onConnect((data) {log("Hello world");});
socket.onConnectError((data) {log(data);});
socket.onError((data) {log(data);});
socket.on("message", (data) {log(data);});
CodePudding user response:
Try adding an options Map
when you initialize your socket
.
io.Socket socket = io.io("http://192.168.1.14:5000", <String, dynamic>{
"transports": ["websocket"],
});
On one of my projects I can confirm that it doesn't work without this being passed in.