Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Reconnect/Reinitialize service #13

Closed
Mourdraug opened this issue Jun 20, 2017 · 5 comments
Closed

Reconnect/Reinitialize service #13

Mourdraug opened this issue Jun 20, 2017 · 5 comments

Comments

@Mourdraug
Copy link

I have spring backend and Angular 2 frontend with websocket communication between. Part of my app have to be accessible anonymously, and other part requires authentication. To achieve this I'd have to remake websocket connection after authentication, because spring security "hands over" authentication to websocket on Upgrade request.
I can disconnect using .disconnect() method, but how can I reconnect after?

@kum-deepak
Copy link
Member

kum-deepak commented Jun 20, 2017

Wow, never knew it worked like that.

There is a try_connect() method in StompService. This however has been marked protected. You can inherit StompService and expose this method in your class.

A longer explanation as to why it is not publicly exposed - this class takes care of automatic re-connection in case of an error. The disconnect call does not clean up the state, so, as soon as it connects after calling try_connect(), it will resubscribe to all previously subscribed queues and send any previously pending outgoing messages. This has advantage that any observers linked to any queues will still work. See if it will works in your scenario.

Another - quite unrelated - approach, do not use Angular's dependency mechanism (at least not conventionally). That will allow you to create a new instance of the Service after a user is authenticated which will be unrelated to the previous one. This is the approach I am taking in running test cases. However it will not retain any previous subscriptions which will need to be subscribed afresh.

This library is designed to allow multiple instances of the service, so, if it makes a cleaner design, you can have two injected instances, one for anonymous access and another for authenticated access.

Let me know if any of the above will work for you.

@Mourdraug
Copy link
Author

Mourdraug commented Jun 21, 2017

Thanks for response,

I tried second approach. Created new Injectable provider that can instantiate new service and I access it through this provider. It works nicely and design is quite clean. If I run into situation where I'll need to keep my observers linked I'll probably try 1st approach too.

Thanks a lot for help.

@kum-deepak
Copy link
Member

Glad it worked. I realized now that several people are using or trying to use this service with Spring Websockets. Many of them facing difficulties. I do not use that environment.

It will be useful if you can contribute a guide on howto use this library with Spring. Also, I realized that some people are trying to use it using SockJS, which is not a great idea in this case.

Your contribution will really help. :)

@Mourdraug
Copy link
Author

There are some problems with setup I use that I still try to resolve, Mainly csrf token that I have to extract from cookie and put in stomp headers, problem is with doing it when upgrade request is first request. Once I have all working I'll upload some guide on how I did it.

@kum-deepak
Copy link
Member

I am sure by now you have been able to configure and use the service. It will be really helpful if you will write a howto. Many thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants