-3

I am using Spring Boot WebSocket notification and Stomp client library in Angular to connect and subscribe to WebSocket. However, sometimes I get the notification on the UI screen and sometimes I do not get the notification. There is flakiness in the behavior.

Can anyone please suggest me the solution?

public getNotification(){
var isConnected = this.notification.initConnnection();
const _this=this;

_this.stompClient.connect({}, function (frame){

const url='topic/topicName';
_this.StompClient.subscribe(url,(message)=>{

console.log(message)
});
},_this.stompCallBack);

)
}

public stopCallBack;any function(error){
console.log(error)
setTimeOut(this.notification,10000)

}

Server side:

@Autowire
SimMessagingTemplate simMessagingTemplate;


simMessagingTemplate.convertAndSend('topic/topicName', message)
Arpit Asati
  • 130
  • 2
  • 16
  • 1
    Add more information to your question. – Sabaoon Bedar Aug 08 '21 at 16:27
  • 1
    What steps have you taken to diagnose the problem? What results did that investigation yield? What have you done to try to fix the problem? – Justin Bertram Aug 08 '21 at 23:32
  • If I know the details you asked in the comment, I could have fixed this by myself. – Arpit Asati Aug 10 '21 at 15:24
  • So you haven't tried to diagnose the problem in *any* way? You haven't done *any* investigation? Not even turned up logging to see what might be different in each case? The question as it stands now is so vague that you're unlikely to get an answer. Even sniffing the network to see if the notification is being sent to the UI would be helpful. Then at least you'd know where to concentrate your debugging efforts. – Justin Bertram Aug 10 '21 at 18:24
  • I tried, ans I saw in the log that websocket connection is keep alive during the session, still there's sometimes ui get notifications sometimes it is not, I'll post the backend and angular code here in sometime, so you will be able to see what I have tried. – Arpit Asati Aug 11 '21 at 17:25
  • Does the log record when the notifications are actually dispatched to clients? – Justin Bertram Aug 12 '21 at 15:45
  • Yes it does, It prints the log when notification dispatched to client but it actually does not reach to the client sometime. – Arpit Asati Aug 13 '21 at 13:45
  • That's *really* important information. You should add that to your question. – Justin Bertram Aug 13 '21 at 14:11
  • Can you add some example code? – Kennuel Aug 14 '21 at 12:01

1 Answers1

-1

Here is an example for configuration:

@Slf4j
@Configuration
@EnableWebSocketMessageBroker
@RequiredArgsConstructor
public class NotificationsWebSocketConfig implements WebSocketMessageBrokerConfigurer {

....

 @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.interceptors(new ChannelInterceptor() {
            @Override
            public Message<?> preSend(Message<?> message, MessageChannel channel) {
                StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
                if (isNull(accessor) || isNull(accessor.getCommand())) {
                    log.warn("Unable to subscribe/unsubscribe: accessor or command is empty.");
                    return message;
                }
                switch (accessor.getCommand()) {
                    case SUBSCRIBE: {
                        wsSessionService.subscribe(accessor);
                        break;
                    }
                    case UNSUBSCRIBE:
                    case DISCONNECT:
                    case ABORT:
                        wsSessionService.unsubscribe(accessor);
                        break;
                }
                return message;
            }
        });
    }

wsSessionService contains some buisness logic. This Service/component stores all active sessions and sends all notifications to destinations using something like this:

@Slf4j
@Service
@RequiredArgsConstructor
public class MessageSender {

    private final ObjectMapper mapper;
    private final SimpMessagingTemplate simpMessagingTemplate;

    @SneakyThrows
    public <T> void sendMessage(String destination, T payload) {
        if (isNull(destination) || isNull(payload)) {
            log.warn("Destination or payload is empty. Cannot send message.");
            return;
        }
        Map<String, Object> headers = new HashMap<>();
        headers.put("content-type", MediaType.APPLICATION_JSON_UTF8);
        MessageHeaders messageHeaders = new MessageHeaders(headers);

        byte[] bytes = mapper.writeValueAsBytes(payload);
        Message<Object> message = MessageBuilder.createMessage(bytes, messageHeaders);

        log.debug("Incoming  message for to destination -> {}", destination);
        simpMessagingTemplate.send(destination, message);
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
yazabara
  • 1,253
  • 4
  • 21
  • 39
  • How does this answer the question about why the UI sometimes doesn't get the notification? – Justin Bertram Aug 15 '21 at 02:31
  • You didn't provide any code. Maybe you didn't store sessions for sending messages, maybe some other issues. It's really hard to fix or suggest something without full infromation or code examples =) I sent examples, which works withpout bugs. (I didn't find) – yazabara Aug 15 '21 at 11:00