WebSocket module for Android crashes app when it fails to connect
When creating a new WebSocket connection, if the var ws = new WebSocket('....'); call fails to connect to the server, it will crash the app by throwing an error with message Cannot close WebSocket. Unknown WebSocket id 0
This is because the websocketFailed event fires, which calls this._closeWebSocket(id); inside WebSocket.js. Since the connection was never open to begin with, calling WebSocket client = mWebSocketConnections.get(id); inside WebSocketModule.java will return null, hence triggering the exception throw new RuntimeException("Cannot close WebSocket. Unknown WebSocket id " + id);
Just to confirm, this is on master?
cc @satya164
Two things we should do: fix the bug and make the native layer uncrashable.
@ide yes, it is on master. I will submit a 1 line PR to fix this bug in the mean time, as it makes WebSockets unusable on Android at the moment.
@yzarubin Than you.
The native layer still crashes with no way to prevent it from js. this happens for me when I enable airplane mode after making a websocket connection. I send a keepalive message every few seconds and when it fails because there is no connection I get this error. RN ~0.42.3
09-04 16:54:00.198 23229 23262 E AndroidRuntime: java.lang.RuntimeException: Cannot send a message. Unknown WebSocket id 2
09-04 16:54:00.198 23229 23262 E AndroidRuntime: at com.facebook.react.modules.websocket.WebSocketModule.send(WebSocketModule.java:221)
09-04 16:54:00.198 23229 23262 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
09-04 16:54:00.198 23229 23262 E AndroidRuntime: at com.facebook.react.bridge.BaseJavaModule$JavaMethod.invoke(BaseJavaModule.java:345)
09-04 16:54:00.198 23229 23262 E AndroidRuntime: at com.facebook.react.cxxbridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:136)
09-04 16:54:00.198 23229 23262 E AndroidRuntime: at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
09-04 16:54:00.198 23229 23262 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java)
09-04 16:54:00.198 23229 23262 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java)
09-04 16:54:00.198 23229 23262 E AndroidRuntime: at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
09-04 16:54:00.198 23229 23262 E AndroidRuntime: at android.os.Looper.loop(Looper.java)
09-04 16:54:00.198 23229 23262 E AndroidRuntime: at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196)
09-04 16:54:00.198 23229 23262 E AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
09-04 16:54:00.208 3551 4476 W ActivityManager: Force finishing activity com.chipta/.MainActivity
I tried a try / catch around the ws.send and also a if this.ws but they both don't prevent this error so I'm kind of stuck. How do I check if I can send a message if the ws object does not give me this information?
edit: It seems possible to prevent this if I check for an internet connection before sending. But it should just raise a js error like a normal http request
@ThaJay i got this error too. People are working with the app outside while they're driving (bad network most of the time) How can we prevent the crash anyway ? 🤕
My comment already states how to prevent the crash:
It seems possible to prevent this if I check for an internet connection before sending. But it should just raise a js error like a normal http request
This should still error in the js and not in the java. Java errors are impossible to deal with from js. So I don't get why this issue was closed. @ide
I just run into this issue in the alpha version of our production app. It's not something widespread yet, one user experienced it two times, but I also would like to know why this was closed when it's still an issue (after 2 years of reporting it).
Sadly I don't have any additional information in helping to solve it. Websockets are used inside subscriptions-transport-ws in my case.

We have hit this as well in our app that is currently in beta, but it is our second most hit crash already. We use websockets in a couple places, and we do the correct hardening in those locations in the javascript. It seems like a bug that the native layer would throw such an exception
We are also experiencing this crash. It happens quite a lot , about 2%-3% of the sessions, to thousands of users. It seems like this is an ongoing issue, maybe it should be re-open?
Reopening since this is somewhat core and results in crashes.
We have this problem Cannot send a message. Unknown WebSocket id ....
crashes sometimes when app starts.
"react": "16.0.0-alpha.12",
"react-i18next": "5.3.0",
"react-native": "0.48.3",
"socket.io": "2.0.3",
"socket.io-client": "2.0.3",
fabric crashlytics stacktrace attached. stacktrace.txt
Same. I get this issue both in debug and production releases on Google Play. I only get this issue on Android. "react": "^16.0.0", "react-native": "^0.49.5"

This issue might be related to #8949
************* Crash Log Head **************** Device Manufacturer: Freescale Device Model : INBOX310 Android Version : 4.2.2 Android SDK : 17 App VersionName : 0.8.7 App VersionCode : 2 ************* Crash Log Head ****************
java.lang.RuntimeException: Cannot send a message. Unknown WebSocket id 0 at com.facebook.react.modules.websocket.WebSocketModule.send(WebSocketModule.java:199) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:363) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:162) at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31) at android.os.Looper.loop(Looper.java:137) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194) at java.lang.Thread.run(Thread.java:856)
https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java#L228
It looks to me as if this exception is the cause of the issue, is there a way we can catch this in JS? throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
I was thinking of just forking RN and just catching this exception natively just to keep my users from experiencing crashes
I was thinking of just forking RN and just catching this exception natively
Or just send a PR?
my problem : in javascript, the socket instance is connected and readyState is OPEN, but when i send a message, android app crash my solution is copy WebSocketModule.java as a CustomModule
if (client == null) {
// This is a programmer error
//throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
mWebSocketConnections.remove(id);
notifyWebSocketFailed(id, "Unknown WebSocket id");
return;
}
native close this socket and let react-native js websocket.onerror or onclose
if (client == null) {
//close this websocket to js websocket.error
}
+1