Skip to content

Commit f3221a4

Browse files
docs: how to create a lot of clients
Related: - #460 - #618
1 parent d178f31 commit f3221a4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/site/markdown/faq.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,32 @@ Socket socket = IO.socket(URI.create("https://example.com"), options);
148148
```
149149

150150
Note: we will upgrade to OkHttp 4 in the next major version.
151+
152+
## How to create a lot of clients
153+
154+
By default, you won't be able to create more than 5 Socket.IO clients (any additional client will be disconnected with "transport error" or "ping timeout" reason). That is due to the default OkHttp [dispatcher](https://square.github.io/okhttp/4.x/okhttp/okhttp3/-dispatcher/), whose `maxRequestsPerHost` is set to 5 by default.
155+
156+
You can overwrite it by providing your own OkHttp client:
157+
158+
```java
159+
int MAX_CLIENTS = 100;
160+
161+
Dispatcher dispatcher = new Dispatcher();
162+
dispatcher.setMaxRequests(MAX_CLIENTS * 2);
163+
dispatcher.setMaxRequestsPerHost(MAX_CLIENTS * 2);
164+
165+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
166+
.dispatcher(dispatcher)
167+
.readTimeout(1, TimeUnit.MINUTES) // important for HTTP long-polling
168+
.build();
169+
170+
IO.Options options = new IO.Options();
171+
options.callFactory = okHttpClient;
172+
options.webSocketFactory = okHttpClient;
173+
174+
for (int i = 0; i < MAX_CLIENTS; i++) {
175+
Socket socket=IO.socket(URI.create("https://example.com"), options);
176+
}
177+
```
178+
179+
Note: we use `MAX_CLIENTS * 2` because a client in HTTP long-polling mode will have one long-running GET request for receiving data from the server, and will create a POST request for sending data to the server.

0 commit comments

Comments
 (0)