Areej Fatimah
Areej Fatimah

Reputation: 261

Java socket io client connection disconnects

I have implemented socket.io client side in java spring boot using https://socketio.github.io/socket.io-client-java/installation.html, with thread pool executor. Now my socket connections are build successfully, but after 50s to 1 minute, they start getting disconnected. How to sustain socket connections? My code is as below

    CountDownLatch lock = new CountDownLatch(200);
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(200);
    ScheduledFuture<?> future;
    for(int i=1;i<=200;i++) {
       
        future= executor.scheduleAtFixedRate(() -> {

            Map<String, String> map = new HashMap<String, String>();
            map.put("token", "abc");

            IO.Options options = IO.Options.builder()
                    .setPath("/socket.io")
                    .setTransports(new String[]{Polling.NAME,WebSocket.NAME})
                    .setAuth(map)
                    .build();

            Socket socket = IO.socket(URI.create("wss://localhost:3000"), options);

            socket.on("connect_error", new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    System.out.println(args[0]);
                }
            });

            socket.on("connect", new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    System.out.println("Socket connection "+i+" successfully created");
                }
            });
            socket.on("disconnect", new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    System.out.println("Socket connection "+i+" disconnected");
                }
            });

            socket.connect();
            lock.countDown();
        }, 1000, delay, TimeUnit.MILLISECONDS);

        lock.await(delay, TimeUnit.MILLISECONDS);
        future.cancel(true);

    }//end of for loop
    executor.shutdown();

My java socket.io-client implementation version is as follow that is compatible with socket.io server v4.x.

<dependency>
<groupId>io.socket</groupId>
<artifactId>socket.io-client</artifactId>
<version>2.1.0</version>
</dependency>

Output

Socket connection 1 successfully created
Socket connection 2 successfully created
Socket connection 3 successfully created
Socket connection 4 successfully created
Socket connection 5 successfully created
Socket connection 6 successfully created
Socket connection 7 successfully created
Socket connection 8 successfully created
Socket connection 9 successfully created
Socket connection 10 successfully created
Socket connection 11 successfully created
Socket connection 12 successfully created
Socket connection 9 disconnected
Socket connection 13 successfully created
....
....

How to prevent these disconnections occuring after few seconds?

Verifying socket.id()

socket.on("connect", new Emitter.Listener() {
   @Override
   public void call(Object... args) {
      System.out.println(socket.id()+" "+i+" connected");
   }
 });
socket.on("disconnect", new Emitter.Listener() {
   @Override
   public void call(Object... args) {
      System.out.println(socket.id()+" "+i+" disconnected");
   }
 });

Output

null 1 connected
null 2 connected
null 3 connected
....
null 4 disconnected
G6yGTZp-AU2Mgy_kAAMl 35 connected
....
G01iFSB2Y98jrwNNAASH 78 disconnected
....
v08qy3AzhbB6V8LXAALL 200 connected

Why socket.id() is returning a null value for some and valid id for some?

Edited : Let's say if thread in a thread pool finishes its task & terminates after 60 s of inactivity, that's casuing the disconnections, then which service executor is used to support long lived thread use cases? Or once a thread terminates and now is idle or waiting, the same is scheduled for next task, which cause disconnection behaviour from previous session? If socket server & client are not sending data in some time, the connection get closed due to this reason?

Upvotes: 2

Views: 754

Answers (0)

Related Questions