reactjswebsocketspring-websocketsockjsjava-websocket

Cannot send message after handshake in SockJsClient (React)


I am using Spring Boot-React JS and trying to send message through WebSocket. Handshake is fine, i can subscribe with no problem. When i try to send data to a topic, does not work. onMessage did not get triggered.

WebSocketConfig

   @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {

        // prefix for server to client message destination
        registry.enableSimpleBroker("/ws-push-notification"); 
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {

        // SockJS connection endpoint
        registry.addEndpoint("/ws-push-notifications").setAllowedOrigins(allowedOrigin).withSockJS();
    }

WebSocketDispatcher

 public void pushNotification(String pushToken, PushNotificationDto notificationDto) {

        SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
        headerAccessor.setSessionId(pushToken);
        headerAccessor.setLeaveMutable(true);

        template.convertAndSendToUser(pushToken, "/ws-push-notification/item/" + pushToken,
                notificationDto, headerAccessor.getMessageHeaders());
    }

I checked data and token, it is matching.

React part:

 const Client = ({
                              props,
                              autoReconnect = true
                          }) => {
    
        const url = "http://localhost:8080/ws-push-notifications?access_token=" + localStorage.getItem(STORAGE_KEY_AT);
        
       const topic = "ws-push-notification/item/" + props.pushToken;
   
        const headers = {
            "Content-Type": CONTENT_TYPE_JSON,
            Authorization: "Bearer " + localStorage.getItem(STORAGE_KEY_AT)
        };
    
        const onConnect = () => {
            props.changeSocketConnectionStatus(true);
            console.info(`Subscribed to socket ${SUDate.toISOLocalDateTime(new Date())}`);
        };
    
        const onMessage = (data) => {
            console.log("This part not getting triggered!!!")
        };

        return (
            <SockJsClient
                url={url}
                topics={[topic]}
                headers={headers}
                subscribeHeaders={headers}
                onConnect={onConnect}
                onMessage={onMessage}
                onDisconnect={onDisconnect}
                onConnectFailure={onConnectFailure}
                autoReconnect={autoReconnect}
            />
        );
    };
    
    export default Client;

template.convertAndSendToUser(pushToken, "/ws-push-notification/item/" + pushToken, notificationDto, headerAccessor.getMessageHeaders());

this send the dto to: /ws-push-notification/item/771063a4-fcea-454f-9673-dedc842290bb905557640

and this part is the same here. const topic = "ws-push-notification/item/" + props.pushToken;

Why is not working?


Solution

  • Problem was not receiving the data. It was sending it.

    template.convertAndSend("/ws-push-notification/item/" + pushToken, notificationDto);
    

    solved my problem.