Socket IO is not connected wen using android 10
Hi team socket.io . I was used socket.io latest version on my android application but its failed to connect server with android 10
kindly help me to sort out this issue
regards Jaaveeth H
I had a similar problem. It was fixed when I installed version 2.3.0
@Jaaveeth are you using react native? Or the java client (which was recently updated to support Socket.IO v3?
Me too. I’m Using [email protected]. It works fine for the web-version (on the desktop and on Android browser too) but if I run React Native app (via Expo Go) which uses the same code, the socket tries to connect and fails immediately. The only possibly useful detail I found is in the description field of the Error object of the connect_error object:
Event {
"isTrusted": false,
"message": "Code 1005 is reserved and may not be used.",
}
A few extra findings. If I run the server with DEBUG="*" I can see the client actually kicks the server door:
[socketio] 2021-03-14T11:30:28.523Z engine handshaking client "JYq5hIKoH4s2DFh_AAAI"
[socketio] 2021-03-14T11:30:28.524Z engine:socket sending packet "open" ({"sid":"JYq5hIKoH4s2DFh_AAAI","upgrades":[],"pingInterval":25000,"pingTimeout":20000})
[socketio] 2021-03-14T11:30:28.524Z engine:socket flushing buffer to transport
[socketio] 2021-03-14T11:30:28.524Z engine:ws writing "0{"sid":"JYq5hIKoH4s2DFh_AAAI","upgrades":[],"pingInterval":25000,"pingTimeout":20000}"
[socketio] 2021-03-14T11:30:28.525Z engine:transport setting request
[socketio] 2021-03-14T11:30:28.525Z socket.io:server incoming connection with id JYq5hIKoH4s2DFh_AAAI
[socketio] 2021-03-14T11:30:28.540Z engine:ws received "40{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NDAwMCwiaWF0IjoxNjE0OTcwNjQ1LCJleHAiOjE2NDY1MDY2NDV9.Zr_iQDt_27rX1_CQ0Xm6gltrKuuXl5rP46aoB-iBGSk"}"
[socketio] 2021-03-14T11:30:28.540Z engine:socket packet
[socketio] 2021-03-14T11:30:28.541Z socket.io-parser decoded 0{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NDAwMCwiaWF0IjoxNjE0OTcwNjQ1LCJleHAiOjE2NDY1MDY2NDV9.Zr_iQDt_27rX1_CQ0Xm6gltrKuuXl5rP46aoB-iBGSk"} as {"type":0,"nsp":"/","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NDAwMCwiaWF0IjoxNjE0OTcwNjQ1LCJleHAiOjE2NDY1MDY2NDV9.Zr_iQDt_27rX1_CQ0Xm6gltrKuuXl5rP46aoB-iBGSk"}}
[socketio] 2021-03-14T11:30:28.541Z socket.io:client connecting to namespace /
[socketio] 2021-03-14T11:30:28.541Z socket.io:namespace adding socket to nsp /
[socketio] 2021-03-14T11:30:28.542Z engine:ws closing
[socketio] 2021-03-14T11:30:28.543Z socket.io:client client close with reason forced close
But look at the last two lines. The server closes the connection immediately (within 2 ms) after accepting it for no obvious reason.
I also have tried the examples/react-native and to my surprise, the example works fine. I also tried to upgrade client & server versions of the example to 4.0.0 and it still works. Then I upgraded Expo & React Native there to match those which are in my project (Expo 39) and it still works.
I have no idea for now what’s different between my project which fails to connect (Android fails, web is OK) and the example. Still searching...
So, the problem I had faced has nothing to do with Android. I thought it was related to React Native or Android because testing on a device has hit a server branch which was not executed under other conditions.
Currently, I have a fully functioning stack with Android 10, React Native on Expo, and Socket.io 4.0.0 (both JS-client and server). I’m not saying all who report Socket.io 3.x and 4.x problems with Android are mistaken but it might be you’ve done one of those errors I’ve overlooked on the server part.
The problem was in socket middleware related to auth. Here’s the diff which resolved the problem:
io.use((socket, next) => {
const token = socket.handshake.auth.token;
jwt.verify(token, jwtSecret, function (err, data) {
const userId = data ? data.id : undefined;
const isAuthorized = userId && !err;
- if (!isAuthorized) {
+ if (isAuthorized) {
+ socket.userId = userId;
+ next();
+ } else {
log.info(
`Rejecting unauthorized websocket connection with token '${token}', error: ${err}.`
);
- const err = new Error("Invalid token");
- err.code = "INVALID_TOKEN";
- next(err);
+ next(new Error("INVALID_TOKEN"));
}
-
- socket.userId = userId;
- next();
});
});
There were three problems at once actually:
- If the token was invalid, the
nextwas invoked two times: one with error, another one without args (so eitherreturnafter fail or make an explicitelsebranch) - There was a variable name collision (
err): one for the resulting error and one for JWT validation. I saw no "Rejecting unauthorized websocket" at all. I suspectjwt.verifyhas intercepted the JS exception and made it silent. Effectivelly,nextwas not invoked at all for some cases - I attached the
codefield to the error and looked at it on the client-side. It is not obvious from docs that onlydatafield will be serialized and passed to the client
These three points are pitfalls that might be better highlighted in the docs. Just a thought.
So, if you have Android problems, double-check your server first. Run it with DEBUG="*" and see what happens. If everything looks fine, try the built-in example examples/react-native. If it works, try to find ten differences with your project or even try to route your app to the demo-server just to establish the connection and see what happens.
Cheers.
For future readers:
Please check the example with React native here: https://github.com/socketio/socket.io/tree/main/examples/react-native
Please reopen if needed :+1:
Any help for flutter?