WebSocketModule uses OkHttpClientProvider (#55885)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/55885

Instead of creating a brand-new OkHttpClient.Builder() per WebSocket
connection, derive from OkHttpClientProvider.getOkHttpClient() so
that all connections share the same connection pool and dispatcher.

Also remove the dispatcher shutdown call, since the dispatcher is now
shared with the global OkHttpClient and shutting it down would kill
all connections.

Changelog: [Internal]

Reviewed By: cipolleschi, javache

Differential Revision: D95059116

fbshipit-source-id: c8052c11f26a07961bcc8e4428508abeb391541d
This commit is contained in:
Nicola Corti authored and meta-codesync[bot] committed 2026-03-04 13:24:08 -08:00
1 parent 3f46e5f279
commit 8487b6d03b
1 file changed
+3 -4
@@ -22,6 +22,7 @@ import com.facebook.react.common.ReactConstants
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.modules.network.CustomClientBuilder
import com.facebook.react.modules.network.ForwardingCookieHandler
import com.facebook.react.modules.network.OkHttpClientProvider
import java.io.IOException
import java.net.URI
import java.net.URISyntaxException
@@ -80,7 +81,8 @@ public class WebSocketModule(context: ReactApplicationContext) :
) {
val id = socketID.toInt()
val okHttpBuilder =
OkHttpClient.Builder()
OkHttpClientProvider.getOkHttpClient()
.newBuilder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.MINUTES) // Disable timeouts for read
@@ -198,9 +200,6 @@ public class WebSocketModule(context: ReactApplicationContext) :
}
},
)
// Trigger shutdown of the dispatcher's executor so this process can exit cleanly
client.dispatcher().executorService().shutdown()
}
override fun close(code: Double, reason: String?, socketID: Double) {