Abubakr Shoaliev
Abubakr Shoaliev

Reputation: 3

Can't connect to my WebSocket when it's running on remote

I created few WebSocket endpoints on my Spring Boot program. I have authorization and authentication in my project too. When I run the program on my local machine, I can easily connect to a WebSocket URL, but when I run the program on my EC2 instance it returns me "503 Service Unavailable" error. REST endpoints work fine when running on remote (like sign-in, or some GET requests).

Could you please guide me on what am I doing wrong? Why only the websocket endpoints don't work when the program is running on remote? Note that I can successfully send HTTP requests. It's only the ws:// endpoints that don't work.

This is the approximate URL that I use to connect to a websocket:

ws://<my_public_ip>:8080/bitget/openInterest?token=eyJhbGciOiJIUzUxMiJ9

This is the handshake details when I attempt to connect to my websocket hosted on remote machine:

Error: Unexpected server response: 503
Handshake Details
Request Method: GET
Status Code: 503 Service Unavailable
Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: IsmT7rtHeLOKadliKnIq/Q==
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: <my_public_IP>:8080
Response Headers
Content-Type: text/html; charset=UTF-8
Content-Length: 6832
Connection: close
P3P: CP="CAO PSA OUR"
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

I have a following websocket configuration:

@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {

    private final WSOrderBookHandler orderBookHandler;
    private final WSOpenInterestHandler openInterestHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(orderBookHandler, "/binance/depth").setAllowedOrigins("*")
                .addHandler(openInterestHandler, "/bitget/openInterest").setAllowedOrigins("*");
    }
}

This is one of my handlers:

@Component
@Slf4j
@RequiredArgsConstructor
public class WSOpenInterestHandler extends TextWebSocketHandler {

    private final Set<WebSocketSession> sessions = ConcurrentHashMap.newKeySet();

    @Override
    public synchronized void afterConnectionEstablished(@NonNull WebSocketSession session) {
        log.info("Open Interest client connected: {}", session.getId());
        sessions.add(session);
    }

    @Override
    public void afterConnectionClosed(@NonNull WebSocketSession session, @NonNull CloseStatus status) {
        log.info("Websocket connection closed: {}", session.getId());
        sessions.remove(session);
    }

    public void broadCastData(String data) {
        sessions.forEach(session -> {
            try {
                session.sendMessage(new TextMessage(data));
            } catch (IOException e) {
                log.error("Couldn't send message: {}", e.getMessage());
            }
        });
    }
}

And this is my security group configuration, if needed

Upvotes: 0

Views: 32

Answers (0)

Related Questions