Repository navigation
Expand file tree
/
Copy pathinitialization.html
More file actions
586 lines (497 loc) · 26.3 KB
/
Copy pathinitialization.html
File metadata and controls
586 lines (497 loc) · 26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/initialization.md at 2025-02-18
| Rendered using Apache Maven Fluido Skin 1.9
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="generator" content="Apache Maven Doxia Site Renderer 1.9.2" />
<title>socket.io-client – Initialization</title>
<link rel="stylesheet" href="./css/apache-maven-fluido-1.9.min.css" />
<link rel="stylesheet" href="./css/site.css" />
<link rel="stylesheet" href="./css/print.css" media="print" />
<script src="./js/apache-maven-fluido-1.9.min.js"></script>
</head>
<body class="topBarDisabled">
<a href="https://github.com/socketio/socket.io-client-java">
<img style="position: absolute; top: 0; right: 0; border: 0; z-index: 10000;"
src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"
alt="Fork me on GitHub">
</a>
<div class="container-fluid">
<header>
<div id="banner">
<div class="pull-left"><div id="bannerLeft"><h2>Socket.IO Java client</h2>
</div>
</div>
<div class="pull-right"></div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 2025-02-18<span class="divider">|</span>
</li>
<li id="projectVersion">Version: 2.1.2</li>
</ul>
</div>
</header>
<div class="row-fluid">
<header id="leftColumn" class="span2">
<nav class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Overview</li>
<li><a href="installation.html" title="Installation"><span class="none"></span>Installation</a></li>
<li class="active"><a href="#"><span class="none"></span>Initialization</a></li>
<li><a href="emitting_events.html" title="Emitting events"><span class="none"></span>Emitting events</a></li>
<li><a href="listening_to_events.html" title="Listening to events"><span class="none"></span>Listening to events</a></li>
<li><a href="socket_instance.html" title="The Socket instance"><span class="none"></span>The Socket instance</a></li>
<li><a href="migrating_from_1_x.html" title="Migrating from 1.x"><span class="none"></span>Migrating from 1.x</a></li>
<li><a href="logging.html" title="Logging"><span class="none"></span>Logging</a></li>
<li><a href="faq.html" title="FAQ"><span class="none"></span>FAQ</a></li>
<li><a href="android.html" title="Android"><span class="none"></span>Android</a></li>
<li class="nav-header">Miscellaneous</li>
<li><a href="changelog.html" title="Changelog"><span class="none"></span>Changelog</a></li>
<li><a href="apidocs/index.html" title="Javadoc"><span class="none"></span>Javadoc</a></li>
<li class="nav-header">Project Documentation</li>
<li><a href="project-info.html" title="Project Information"><span class="icon-chevron-right"></span>Project Information</a></li>
</ul>
</nav>
<div class="well sidebar-nav">
<hr />
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy"><img class="builtBy" alt="Built by Maven" src="./images/logos/maven-feather.png" /></a>
</div>
</div>
</header>
<main id="bodyColumn" class="span10" >
<h1>Initialization</h1>
<p><b>Table of content</b></p>
<ul>
<li><a href="#Creation_of_a_Socket_instance">Creation of a Socket instance</a></li>
<li><a href="#Default_values">Default values</a></li>
<li><a href="#Description">Description</a>
<ul>
<li><a href="#IO_factory_options">IO factory options</a>
<ul>
<li><a href="#forceNew">forceNew</a></li>
<li><a href="#multiplex">multiplex</a></li></ul></li>
<li><a href="#Low-level_engine_options">Low-level engine options</a>
<ul>
<li><a href="#transports">transports</a></li>
<li><a href="#upgrade">upgrade</a></li>
<li><a href="#rememberUpgrade">rememberUpgrade</a></li>
<li><a href="#path">path</a></li>
<li><a href="#query">query</a></li>
<li><a href="#extraHeaders">extraHeaders</a></li>
<li><a href="#callFactory">callFactory</a></li>
<li><a href="#webSocketFactory">webSocketFactory</a></li></ul></li>
<li><a href="#Manager_options">Manager options</a>
<ul>
<li><a href="#reconnection">reconnection</a></li>
<li><a href="#reconnectionAttempts">reconnectionAttempts</a></li>
<li><a href="#reconnectionDelay">reconnectionDelay</a></li>
<li><a href="#reconnectionDelayMax">reconnectionDelayMax</a></li>
<li><a href="#randomizationFactor">randomizationFactor</a></li>
<li><a href="#timeout">timeout</a></li></ul></li>
<li><a href="#Socket_options">Socket options</a>
<ul>
<li><a href="#auth">auth</a></li></ul></li></ul></li>
<li><a href="#SSL_connections">SSL connections</a>
<ul>
<li><a href="#With_a_keystore">With a keystore</a></li>
<li><a href="#Trust_all_certificates">Trust all certificates</a></li></ul></li>
<li><a href="#Multiplexing">Multiplexing</a></li></ul>
<section>
<h2><a name="Creation_of_a_Socket_instance"></a>Creation of a Socket instance</h2>
<div class="source">
<div class="source"><pre class="prettyprint">URI uri = URI.create("https://example.com");
IO.Options options = IO.Options.builder()
// ...
.build();
Socket socket = IO.socket(uri, options);
</pre></div></div>
<p>Unlike the JS client (which can infer it from the <code>window.location</code> object), the URI is mandatory here.</p>
<p>The <a class="externalLink" href="https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#Syntax">scheme</a> part of the URI is also mandatory. Both <code>ws://</code> and <code>http://</code> can be used interchangeably.</p>
<div class="source">
<div class="source"><pre class="prettyprint">Socket socket = IO.socket("https://example.com"); // OK
Socket socket = IO.socket("wss://example.com"); // OK, similar to the example above
Socket socket = IO.socket("192.168.0.1:1234"); // NOT OK, missing the scheme part
</pre></div></div>
<p>The path represents the <a class="externalLink" href="https://socket.io/docs/v4/namespaces/">Namespace</a>, and not the actual path (see <a href="#path">below</a>) of the HTTP requests:</p>
<div class="source">
<div class="source"><pre class="prettyprint">Socket socket = IO.socket(URI.create("https://example.com")); // the main namespace
Socket productSocket = IO.socket(URI.create("https://example.com/product")); // the "product" namespace
Socket orderSocket = IO.socket(URI.create("https://example.com/order")); // the "order" namespace
</pre></div></div>
</section><section>
<h2><a name="Default_values"></a>Default values</h2>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
// IO factory options
.setForceNew(false)
.setMultiplex(true)
// low-level engine options
.setTransports(new String[] { Polling.NAME, WebSocket.NAME })
.setUpgrade(true)
.setRememberUpgrade(false)
.setPath("/socket.io/")
.setQuery(null)
.setExtraHeaders(null)
// Manager options
.setReconnection(true)
.setReconnectionAttempts(Integer.MAX_VALUE)
.setReconnectionDelay(1_000)
.setReconnectionDelayMax(5_000)
.setRandomizationFactor(0.5)
.setTimeout(20_000)
// Socket options
.setAuth(null)
.build();
</pre></div></div>
</section><section>
<h2><a name="Description"></a>Description</h2><section>
<h3><a name="IO_factory_options"></a>IO factory options</h3>
<p>These settings will be shared by all Socket instances attached to the same Manager.</p><section>
<h4><a name="forceNew"></a><code>forceNew</code></h4>
<p>Default value: <code>false</code></p>
<p>Whether to create a new Manager instance.</p>
<p>A Manager instance is in charge of the low-level connection to the server (established with HTTP long-polling or WebSocket). It handles the reconnection logic.</p>
<p>A Socket instance is the interface which is used to sends events to — and receive events from — the server. It belongs to a given <a class="externalLink" href="https://socket.io/docs/v4/namespaces">namespace</a>.</p>
<p>A single Manager can be attached to several Socket instances.</p>
<p>The following example will reuse the same Manager instance for the 3 Socket instances (one single WebSocket connection):</p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setForceNew(false)
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options); // the main namespace
Socket productSocket = IO.socket(URI.create("https://example.com/product"), options); // the "product" namespace
Socket orderSocket = IO.socket(URI.create("https://example.com/order"), options); // the "order" namespace
</pre></div></div>
<p>The following example will create 3 different Manager instances (and thus 3 distinct WebSocket connections):</p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setForceNew(true)
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options); // the main namespace
Socket productSocket = IO.socket(URI.create("https://example.com/product"), options); // the "product" namespace
Socket orderSocket = IO.socket(URI.create("https://example.com/order"), options); // the "order" namespace
</pre></div></div>
</section><section>
<h4><a name="multiplex"></a><code>multiplex</code></h4>
<p>Default value: <code>true</code></p>
<p>The opposite of <code>forceNew</code>: whether to reuse an existing Manager instance.</p></section></section><section>
<h3><a name="Low-level_engine_options"></a>Low-level engine options</h3><section>
<h4><a name="transports"></a><code>transports</code></h4>
<p>Default value: <code>new String[] { Polling.NAME, WebSocket.NAME }</code></p>
<p>The low-level connection to the Socket.IO server can either be established with:</p>
<ul>
<li>HTTP long-polling: successive HTTP requests (<code>POST</code> for writing, <code>GET</code> for reading)</li>
<li><a class="externalLink" href="https://en.wikipedia.org/wiki/WebSocket">WebSocket</a></li>
</ul>
<p>The following example disables the HTTP long-polling transport:</p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setTransports(new String[] { WebSocket.NAME })
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
<p>Note: in that case, sticky sessions are not required on the server side (more information <a class="externalLink" href="https://socket.io/docs/v4/using-multiple-nodes/">here</a>).</p></section><section>
<h4><a name="upgrade"></a><code>upgrade</code></h4>
<p>Default value: <code>true</code></p>
<p>Whether the client should try to upgrade the transport from HTTP long-polling to something better.</p></section><section>
<h4><a name="rememberUpgrade"></a><code>rememberUpgrade</code></h4>
<p>Default value: <code>false</code></p>
<p>If true and if the previous WebSocket connection to the server succeeded, the connection attempt will bypass the normal upgrade process and will initially try WebSocket. A connection attempt following a transport error will use the normal upgrade process. It is recommended you turn this on only when using SSL/TLS connections, or if you know that your network does not block websockets.</p></section><section>
<h4><a name="path"></a><code>path</code></h4>
<p>Default value: <code>/socket.io/</code></p>
<p>It is the name of the path that is captured on the server side.</p>
<p>The server and the client values must match:</p>
<p><i>Client</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setPath("/my-custom-path/")
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
<p><i>JavaScript Server</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">import { Server } from "socket.io";
const io = new Server(8080, {
path: "/my-custom-path/"
});
io.on("connection", (socket) => {
// ...
});
</pre></div></div>
<p>Please note that this is different from the path in the URI, which represents the <a class="externalLink" href="https://socket.io/docs/v4/namespaces/">Namespace</a>.</p>
<p>Example:</p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setPath("/my-custom-path/")
.build();
Socket socket = IO.socket(URI.create("https://example.com/order"), options);
</pre></div></div>
<ul>
<li>the Socket instance is attached to the “order” Namespace</li>
<li>the HTTP requests will look like: <code>GET https://example.com/my-custom-path/?EIO=4&transport=polling&t=ML4jUwU</code></li>
</ul></section><section>
<h4><a name="query"></a><code>query</code></h4>
<p>Default value: -</p>
<p>Additional query parameters (then found in <code>socket.handshake.query</code> object on the server-side).</p>
<p>Example:</p>
<p><i>Client</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setQuery("x=42")
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
<p><i>JavaScript Server</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">io.on("connection", (socket) => {
console.log(socket.handshake.query); // prints { x: '42', EIO: '4', transport: 'polling' }
});
</pre></div></div>
<p>Note: The <code>socket.handshake.query</code> object contains the query parameters that were sent during the Socket.IO handshake, it won’t be updated for the duration of the current session, which means changing the <code>query</code> on the client-side will only be effective when the current session is closed and a new one is created:</p>
<div class="source">
<div class="source"><pre class="prettyprint">socket.io().on(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
@Override
public void call(Object... args) {
options.query = "y=43";
}
});
</pre></div></div>
</section><section>
<h4><a name="extraHeaders"></a><code>extraHeaders</code></h4>
<p>Default value: -</p>
<p>Additional headers (then found in <code>socket.handshake.headers</code> object on the server-side).</p>
<p>Example:</p>
<p><i>Client</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setExtraHeaders(singletonMap("authorization", singletonList("bearer 1234")))
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
<p><i>JavaScript Server</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">io.on("connection", (socket) => {
console.log(socket.handshake.headers); // prints { accept: '*/*', authorization: 'bearer 1234', connection: 'Keep-Alive', 'accept-encoding': 'gzip', 'user-agent': 'okhttp/3.12.12' }
});
</pre></div></div>
<p>Note: Similar to the <code>query</code> option above, the <code>socket.handshake.headers</code> object contains the headers that were sent during the Socket.IO handshake, it won’t be updated for the duration of the current session, which means changing the <code>extraHeaders</code> on the client-side will only be effective when the current session is closed and a new one is created:</p>
<div class="source">
<div class="source"><pre class="prettyprint">socket.io().on(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
@Override
public void call(Object... args) {
options.extraHeaders.put("authorization", singletonList("bearer 5678"));
}
});
</pre></div></div>
</section><section>
<h4><a name="callFactory"></a><code>callFactory</code></h4>
<p>The <a class="externalLink" href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/">OkHttpClient instance</a> to use for HTTP long-polling requests.</p>
<div class="source">
<div class="source"><pre class="prettyprint">OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(1, TimeUnit.MINUTES) // important for HTTP long-polling
.build();
IO.Options options = new IO.Options();
options.callFactory = okHttpClient;
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
</section><section>
<h4><a name="webSocketFactory"></a><code>webSocketFactory</code></h4>
<p>The <a class="externalLink" href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/">OkHttpClient instance</a> to use for WebSocket connections.</p>
<div class="source">
<div class="source"><pre class="prettyprint">OkHttpClient okHttpClient = new OkHttpClient.Builder()
.minWebSocketMessageToCompress(2048)
.build();
IO.Options options = new IO.Options();
options.webSocketFactory = okHttpClient;
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
</section></section><section>
<h3><a name="Manager_options"></a>Manager options</h3>
<p>These settings will be shared by all Socket instances attached to the same Manager.</p><section>
<h4><a name="reconnection"></a><code>reconnection</code></h4>
<p>Default value: <code>true</code></p>
<p>Whether reconnection is enabled or not. If set to <code>false</code>, you need to manually reconnect.</p></section><section>
<h4><a name="reconnectionAttempts"></a><code>reconnectionAttempts</code></h4>
<p>Default value: <code>Integer.MAX_VALUE</code></p>
<p>The number of reconnection attempts before giving up.</p></section><section>
<h4><a name="reconnectionDelay"></a><code>reconnectionDelay</code></h4>
<p>Default value: <code>1_000</code></p>
<p>The initial delay before reconnection in milliseconds (affected by the <a href="#randomizationfactor">randomizationFactor</a> value).</p></section><section>
<h4><a name="reconnectionDelayMax"></a><code>reconnectionDelayMax</code></h4>
<p>Default value: <code>5_000</code></p>
<p>The maximum delay between two reconnection attempts. Each attempt increases the reconnection delay by 2x.</p></section><section>
<h4><a name="randomizationFactor"></a><code>randomizationFactor</code></h4>
<p>Default value: <code>0.5</code></p>
<p>The randomization factor used when reconnecting (so that the clients do not reconnect at the exact same time after a server crash, for example).</p>
<p>Example with the default values:</p>
<ul>
<li>1st reconnection attempt happens between 500 and 1500 ms (<code>1000 * 2^0 * (<something between -0.5 and 1.5>)</code>)</li>
<li>2nd reconnection attempt happens between 1000 and 3000 ms (<code>1000 * 2^1 * (<something between -0.5 and 1.5>)</code>)</li>
<li>3rd reconnection attempt happens between 2000 and 5000 ms (<code>1000 * 2^2 * (<something between -0.5 and 1.5>)</code>)</li>
<li>next reconnection attempts happen after 5000 ms</li>
</ul></section><section>
<h4><a name="timeout"></a><code>timeout</code></h4>
<p>Default value: <code>20_000</code></p>
<p>The timeout in milliseconds for each connection attempt.</p></section></section><section>
<h3><a name="Socket_options"></a>Socket options</h3>
<p>These settings are specific to the given Socket instance.</p><section>
<h4><a name="auth"></a><code>auth</code></h4>
<p>Default value: -</p>
<p>Credentials that are sent when accessing a namespace (see also <a class="externalLink" href="https://socket.io/docs/v4/middlewares/#sending-credentials">here</a>).</p>
<p>Example:</p>
<p><i>Client</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setAuth(singletonMap("token", "abcd"))
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
<p><i>JavaScript Server</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">io.on("connection", (socket) => {
console.log(socket.handshake.auth); // prints { token: 'abcd' }
});
</pre></div></div>
<p>You can update the <code>auth</code> map when the access to the Namespace is denied:</p>
<div class="source">
<div class="source"><pre class="prettyprint">socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
options.auth.put("token", "efgh");
socket.connect();
}
});
</pre></div></div>
<p>Or manually force the Socket instance to reconnect:</p>
<div class="source">
<div class="source"><pre class="prettyprint">options.auth.put("token", "efgh");
socket.disconnect().connect();
</pre></div></div>
</section></section></section><section>
<h2><a name="SSL_connections"></a>SSL connections</h2><section>
<h3><a name="With_a_keystore"></a>With a keystore</h3>
<div class="source">
<div class="source"><pre class="prettyprint">HostnameVerifier hostnameVerifier = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession sslSession) {
return hostname.equals("example.com");
}
};
KeyStore ks = KeyStore.getInstance("JKS");
File file = new File("path/to/the/keystore.jks");
ks.load(new FileInputStream(file), "password".toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, "password".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.hostnameVerifier(hostnameVerifier)
.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) tmf.getTrustManagers()[0])
.readTimeout(1, TimeUnit.MINUTES) // important for HTTP long-polling
.build();
IO.Options options = new IO.Options();
options.callFactory = okHttpClient;
options.webSocketFactory = okHttpClient;
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
</section><section>
<h3><a name="Trust_all_certificates"></a>Trust all certificates</h3>
<p>Please use with caution, as this defeats the whole purpose of using secure connections.</p>
<p>This is equivalent to <code>rejectUnauthorized: false</code> for the JavaScript client.</p>
<div class="source">
<div class="source"><pre class="prettyprint">HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession sslSession) {
return true;
}
};
X509TrustManager trustManager = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) {
// not implemented
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) {
// not implemented
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { trustManager }, null);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.hostnameVerifier(hostnameVerifier)
.sslSocketFactory(sslContext.getSocketFactory(), trustManager)
.readTimeout(1, TimeUnit.MINUTES) // important for HTTP long-polling
.build();
IO.Options options = new IO.Options();
options.callFactory = okHttpClient;
options.webSocketFactory = okHttpClient;
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
</section></section><section>
<h2><a name="Multiplexing"></a>Multiplexing</h2>
<p>The Java client does support multiplexing: this allows to split the logic of your application into distinct modules, while using one single WebSocket connection to the server.</p>
<p>Reference: <a class="externalLink" href="https://socket.io/docs/v4/namespaces/">https://socket.io/docs/v4/namespaces/</a></p>
<div class="source">
<div class="source"><pre class="prettyprint">Socket socket = IO.socket(URI.create("https://example.com")); // the main namespace
Socket productSocket = IO.socket(URI.create("https://example.com/product")); // the "product" namespace
Socket orderSocket = IO.socket(URI.create("https://example.com/order")); // the "order" namespace
// all 3 sockets share the same Manager
System.out.println(socket.io() == productSocket.io()); // true
System.out.println(socket.io() == orderSocket.io()); // true
</pre></div></div>
<p>Please note that multiplexing will be disabled in the following cases:</p>
<ul>
<li>multiple creation for the same namespace</li>
</ul>
<div class="source">
<div class="source"><pre class="prettyprint">Socket socket = IO.socket(URI.create("https://example.com"));
Socket socket2 = IO.socket(URI.create("https://example.com"));
System.out.println(socket.io() == socket2.io()); // false
</pre></div></div>
<ul>
<li>different domains</li>
</ul>
<div class="source">
<div class="source"><pre class="prettyprint">Socket socket = IO.socket(URI.create("https://first.example.com"));
Socket socket2 = IO.socket(URI.create("https://second.example.com"));
System.out.println(socket.io() == socket2.io()); // false
</pre></div></div>
<ul>
<li>usage of the <a href="#forceNew">forceNew</a> option</li>
</ul>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setForceNew(true)
.build();
Socket socket = IO.socket(URI.create("https://example.com"));
Socket socket2 = IO.socket(URI.create("https://example.com/admin"), options);
System.out.println(socket.io() == socket2.io()); // false
</pre></div></div></section>
</main>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p>© 2025
</p>
</div>
</div>
</footer>
</body>
</html>