WebSocket Connection Error with Spring Cloud Gateway and Socket.IO

35 Views Asked by At

I am currently working on a project that involves setting up two separate services:

  • A REST API built with Spring Boot
  • A WebSocket service using Socket.IO and Node.js As an API Gateway, I'm using Spring Cloud Gateway. My goal is to route WebSocket traffic to the Socket.IO backend and HTTP traffic to the Spring Boot REST API.

This is what I added to the Spring Cloud Gateway

@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
            .route(p -> p
                .path("/api")
                .uri("http://localhost:8080"))
            .route(p -> p
                    .path("/ws")
                    .uri("ws://localhost:3000"))
            .build();
}

However, when I attempt to establish a WebSocket connection through the Spring Cloud Gateway to the Socket.IO server, the connection fails immediately, and I receive the following error:

io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException: Connection prematurely closed BEFORE opening handshake is complete.
    at reactor.netty.http.client.WebsocketClientOperations.onInboundClose(WebsocketClientOperations.java:184) ~[reactor-netty-http-1.1.17.jar:1.1.17]

This issue occurs as soon as I try to connect, resulting in an immediate disconnection from the Socket.IO server

Is there a solution for this issue ? or should I try another approach ?

0

There are 0 best solutions below