Techiio-author
Started by Bruce MccullamOct 6, 2021

Open
How to add a interceptor to a WebSocket

1 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWES SHARE

I am using a library on my Spring Boot application and this library is exposing a WebSocket endpoint for the clients to communicate. In my use case, I want to enrich the data this library is sending on the WebSocket. I'm thinking of adding a WebSocket interceptor to capture the data and manipulate it, but I am not sure whether it's possible to add an interceptor to a WebSocket endpoint that is exposed by a third party library.

1 Replies

Techiio-commentatorShuvhojit Deb replied 2 months ago0 likes0 dislikes

With a HandshakeInterceptor you won't be able to get CONNECT / DISCONNECT frames. You have to implement a ChannelInterceptor (or extend ChannelInterceptorAdapter) and add it to the clientInboundChannel. The preSend method allows you to add your logic before the message is processed:

public class FilterChannelInterceptor extends ChannelInterceptorAdapter {
    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {
        StompHeaderAccessor headerAccessor= StompHeaderAccessor.wrap(message);
        if (StompCommand.SUBSCRIBE.equals(headerAccessor.getCommand()) {
            // Your logic
        }
        return message;
    }
}
You must be Logged in to reply
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Recommended Blogs
1
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance