Skip to content

Commit 7375763

Browse files
docs: multiplexing
Related: #716
1 parent 6e08d1f commit 7375763

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Diff for: src/site/markdown/initialization.md

+49
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,52 @@ options.webSocketFactory = okHttpClient;
475475

476476
Socket socket = IO.socket(URI.create("https://example.com"), options);
477477
```
478+
479+
## Multiplexing
480+
481+
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.
482+
483+
Reference: https://socket.io/docs/v4/namespaces/
484+
485+
```java
486+
Socket socket = IO.socket(URI.create("https://example.com")); // the main namespace
487+
Socket productSocket = IO.socket(URI.create("https://example.com/product")); // the "product" namespace
488+
Socket orderSocket = IO.socket(URI.create("https://example.com/order")); // the "order" namespace
489+
490+
// all 3 sockets share the same Manager
491+
System.out.println(socket.io() == productSocket.io()); // true
492+
System.out.println(socket.io() == orderSocket.io()); // true
493+
```
494+
495+
Please note that multiplexing will be disabled in the following cases:
496+
497+
- multiple creation for the same namespace
498+
499+
```java
500+
Socket socket = IO.socket(URI.create("https://example.com"));
501+
Socket socket2 = IO.socket(URI.create("https://example.com"));
502+
503+
System.out.println(socket.io() == socket2.io()); // false
504+
```
505+
506+
- different domains
507+
508+
```java
509+
Socket socket = IO.socket(URI.create("https://first.example.com"));
510+
Socket socket2 = IO.socket(URI.create("https://second.example.com"));
511+
512+
System.out.println(socket.io() == socket2.io()); // false
513+
```
514+
515+
- usage of the [forceNew](#forceNew) option
516+
517+
```java
518+
IO.Options options = IO.Options.builder()
519+
.setForceNew(true)
520+
.build();
521+
522+
Socket socket = IO.socket(URI.create("https://example.com"));
523+
Socket socket2 = IO.socket(URI.create("https://example.com/admin"), options);
524+
525+
System.out.println(socket.io() == socket2.io()); // false
526+
```

0 commit comments

Comments
 (0)