stomp-js / ng2-stompjs

Angular 6 and 7 - Stomp service over Websockets
https://stomp-js.github.io/
Apache License 2.0
182 stars 32 forks source link

Updating the brokerUrl of existing connections #250

Closed rjculaway closed 2 years ago

rjculaway commented 2 years ago

Hello,

I am new to web sockets and would really appreciate your help. I have a query parameter in my brokerUrl that frequently changes. Every time value of that query parameter changes I try to update the brokerUrl through the before connect function, but i keep getting this connection failure, and upon checking the query parameter, it still uses the old parameter I used in my brokerUrl.

`

export class StompConfig extends InjectableRxStompConfig { private readonly webSocketUrl = '/api/v1/ws/connect?authorization=';

token: string;

constructor(private settings: SettingsService) {
    super();
    this.configureStomp();
}

private configureStomp() {
    this.brokerURL = `wss://${environment.hostName}${this.webSocketUrl}${this.settings.getKey()}`;
    this.heartbeatIncoming = 0;
    this.heartbeatOutgoing = 40000;
    this.reconnectDelay = 500;
    this.logRawCommunication = true;

    this.beforeConnect = (client: any): Promise<void> => {
        return new Promise<void>((resolve, _) => {
            client.brokerURL = `wss://${environment.hostName}${this.webSocketUrl}${this.settings.getKey()}`;
            resolve();
        });
    };

    this.debug = (message: string) => {
        console.warn(new Date());
        console.warn(message);
        console.warn('----------------------------------- \n -----------------------------------');
    };
}

} `

Here's the code I am using, and the error i'm getting image

kum-deepak commented 2 years ago

I can notice one possible issue. To start with set the type of client in the callback to RxStomp. The property brokerURL needs to be set through the call to https://stomp-js.github.io/api-docs/latest/classes/RxStomp.html#configure, you should call this method with only brokerURL key. Schematic code (untested):

    this.beforeConnect = (client: RxStomp): void|Promise<void> => {
        return new Promise<void>((resolve, _) => {
            client.configure({brokerURL: `wss://${environment.hostName}${this.webSocketUrl}${this.settings.getKey()}`});
            resolve();
        });
    };

Since your method is actually not async, the following also might work:

    this.beforeConnect = (client: RxStomp): void|Promise<void> => {
        client.configure({brokerURL: `wss://${environment.hostName}${this.webSocketUrl}${this.settings.getKey()}`});
    };
rjculaway commented 2 years ago

Hello Deepak,

Thank you for the prompt response. I have tried your solution and it seems to have solved my problem. Thank you so much for your help!