davinkevin / AngularStompDK

Angular service to communicate to Stomp-Websocket
http://davinkevin.github.io/AngularStompDK/
Apache License 2.0
36 stars 12 forks source link

Unable to subscribe user notification #48

Closed LouisWayne closed 7 years ago

LouisWayne commented 7 years ago

I am using Spring Framework, and trying to subscribe 'user' specific notification. Here is the code from JS:

After sign-in process, the client will run the below script

ngstomp.subscribeTo('/user/queue/message').callback(function(response) {
       console.log('test');
    })
.withBodyInJson()
.connect();

But from the server-side, it fails to get the current logged-in user.

@Component
class StompSubscribeEventListener implements ApplicationListener<SessionSubscribeEvent> {
    @Override
    public void onApplicationEvent(SessionSubscribeEvent sessionSubscribeEvent) {
        StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(sessionSubscribeEvent.getMessage());
        System.out.println(sessionSubscribeEvent.getUser()); // null printed
    }
}

When I refresh the page, sessionSubscribeEvent.getUser() gets the current user properly.

Do I need to write special logic since it's subscription for specific user (i.e. header information)? Please advice...!

davinkevin commented 7 years ago

I don't work with user subscription on my project using AngularStomDK, so I didn't have this kind of problem.

Can you do a PR on the project https://github.com/davinkevin/spring-websocket-backend where I use to test the lib and show me the problem ? So I can reproduce the problem and try to implement the solution.

BTW, in your application, do you try to use the settings.autoConnect provider's param to prevent connection on creation of the service and run the connect method of the service after the user identification ? This could be a solution...

LouisWayne commented 7 years ago

@davinkevin , thanks for the reply.

I figured out that the issue is from Spring security. Since it re-assigns the session after signing in, the websocket doesn't really know the session has been changed. There are 2 solutions: 1st one is that using a token as authentication so that the session never recreated. 2nd solution is that keep the session info using REDIS (or something). Since the problem is not from your library, I am going to close the ticket. I love the library, simple to use - thanks again!

By the way, I was testing the method: convertAndSendToUser()