-
Notifications
You must be signed in to change notification settings - Fork 32
how to stop auto web-socket connection and do it manually #121
Comments
I will write an example soon. Meanwhile please see stomp-js/rx-stomp#495 |
Thank you, Deepak. one more thing I want to know. this implementation works fine but whenever page reloaded, WebSocket connection established again so I lost the previous data of the previous topic/channel. How can I persist the connection? I wanted to utilize beforeConnect or mport { Message, Frame, Client } from '@stomp/stompjs';
import { RxStompService, InjectableRxStompConfig } from '@stomp/ng2-stompjs';
import { RxStompState } from '@stomp/rx-stomp'; I know the above method are part of
then later
but it never print anything in console? why so |
RxStompService extends RxStomp and InjectableRxStompConfig extends RxStompConfig. So, simplly add Please note that https://stomp-js.github.io/api-docs/latest/classes/RxStomp.html#configure can be called multiple times. |
But there is no such method as Also, I have tried to call import { RxStompService, InjectableRxStompConfig } from '@stomp/ng2-stompjs';
export class NotificationComponent implements OnInit, OnDestroy, AfterContentInit {
constructor(
private rxStompService: RxStompService,
private rxStompConfig: InjectableRxStompConfig
) {}
ngAfterContentInit() {
this.initStomp();
}
private initStomp() {
this.checkConnection();
const stompConfig: InjectableRxStompConfig = Object.assign({}, myRxStompConfig, {
connectHeaders: {
access_token: sessionStorage.getItem('token')
}
});
this.rxStompService.configure(stompConfig);
this.rxStompService.activate();
// other things to do
}
private checkConnection() {
this.rxStompConfig.beforeConnect = () => {
console.log('%c called before connect', 'color: blue');
};
}
} |
Please change your code as follows and try: import { RxStompService, InjectableRxStompConfig } from '@stomp/ng2-stompjs';
export class NotificationComponent implements OnInit, OnDestroy, AfterContentInit {
constructor(
private rxStompService: RxStompService,
private rxStompConfig: InjectableRxStompConfig
) {}
ngAfterContentInit() {
this.initStomp();
}
private initStomp() {
// Replace if still needed
// this.checkConnection();
const stompConfig: InjectableRxStompConfig = Object.assign({}, myRxStompConfig, {
connectHeaders: {
access_token: sessionStorage.getItem('token')
},
beforeConnect: () => {
console.log('%c called before connect', 'color: blue');
}
});
this.rxStompService.configure(stompConfig);
this.rxStompService.activate();
// other things to do
}
} The underlying this.rxStompService.connected$.subscribe(() => {
// your code
}); |
Thanks for quick reply, I figure it out how to work and did the same as you mentioned. checkConnection() {
this.rxStompService.connected$.subscribe((state: RxStompState) => {
console.log({ state });
this.connectionStatus = RxStompState[state];
});
} but the actual need of this is I want to set the maximum retry of WebSocket or to prevent the reconnection if already connected. How do I archive this? |
You may use https://stomp-js.github.io/api-docs/latest/classes/RxStomp.html#connected to check if it is connected. |
I know this method but how does this help when page reload/refresh? Will you suggest an approach with stackblitz |
It will return |
@kum-deepak Any update on this documentation.? |
@ibhargav90 what specifically are you looking for? |
Looking for this,
On page reload, how can i persist connection. |
It is not possible to persist the connection on a page reload. |
@kum-deepak The connection is closed after sometime automatically, i want to unsubscribe all the topics and connectedState so we can avoid multiple subscription? |
Your code has issues and is likely to lead to issues like multiple subscriptions and leaks. Unless your requirements are very specific, please follow the tutorial (https://stomp-js.github.io/guide/ng2-stompjs/2018/11/04/ng2-stomp-with-angular7.html) and sample (https://github.com/stomp-js/ng2-stompjs-angular7). These are based on our production applications. The library itself does not disconnect on its own, there is likely to be some other reason (please keep that discussion in #146). |
Hi Deepak, I tried to use the AfterContentInit at app.component level, but the connection is already open. What I need to achieve: do not connect if the client is not completely configured |
@mauicoder I am making certain assumptions - you are on latest version, you are using something like following in the providers: providers: [
{
provide: InjectableRxStompConfig,
useValue: myRxStompConfig
},
{
provide: RxStompService,
useFactory: rxStompServiceFactory,
deps: [InjectableRxStompConfig]
}
] Change the above to: providers: [
RxStompService
] With this change, the created instance will not attempt to connect. When you have all required information, call the following to attempt connection: stompService.configure(config);
stompService.activate(); Basically in this mode, the client remains inactive until |
Thanks for your answer Deepak |
Hi, I have a question about error handling with this library. I have something like: startConnetionProcess() {
} where connect() calls a method that executes configure() and activate(). My problem comes when there is an error on the server side. If this connection is not established, I can see it if I have the debug option on my stompConfig config; however, inside of debug, i cannot call any functions apparently. I am using Angular 8. How can I handle an error? I haven't seen any references to error handling. |
Hi, Is there any example to provide stompConfiguration using Angular version: 9.1.3 Error:
|
Please refer to the guide https://stomp-js.github.io/guide/ng2-stompjs/ng2-stomp-with-angular7.html and the sample at https://github.com/stomp-js/ng2-stompjs-angular7. I have recently tested - these still work for latest Angular and latest version of this library. Currently you seem to be using To your main question - please see #194 (comment). |
Hi Deepak, Thanks for your quick response. I have already gone through the above mentioned docs and sample but no where i am able to find an actual implementation of Here you can find the full code
|
Your code should look like following: import {
InjectableRxStompConfig,
RxStompService,
rxStompServiceFactory,
} from '@stomp/ng2-stompjs';
import { myRxStompConfig } from './rx-stomp.config';
import * as SockJS from 'sockjs-client'
..................
@NgModule({
providers: [
{
provide: InjectableRxStompConfig,
useFactory: initConfig,
multi: true
},
{
provide: RxStompService,
useFactory: rxStompServiceFactory,
deps: [InjectableRxStompConfig],
}
]
})
export class AppModule {}
export function initConfig(): Function {
const myRxStompConfig: InjectableRxStompConfig = {
webSocketFactory: () => new SockJS("https://some-domain.com/BASE_URL/ws")
// other settings
};
return myRxStompConfig;
} |
For the life of me, I cannot get this to work. I am presented with the error: Error: Uncaught (in promise): SyntaxError: Failed to construct 'WebSocket': The URL 'undefined' is invalid. My init function looks like the following: app.module
core.module (module where RxStomp lives)
|
Please attach your debug messages. |
Below is the single debug message:
Turns out that when I remove |
I do not understand the use of I am closing the issue for now. If you are still facing the issue, please open a new ticket. |
In your example, WebSocket connection is done automatically. I want to call before connect and other things also mentioned the same that "In this sample, STOMP broker is connected during the Angular DI class initialization phase"
How to call the connection manually in one of my components as soon as that component getting loaded in the page.
can we have sample example?
The text was updated successfully, but these errors were encountered: