Stompjs create sequential double connection
I'm using Stompjs and this is my connect() method
connect() {
let socket = new SockJs(`http://localhost:8084/socket`);
let stompClient = Stomp.over(socket);
return stompClient;
}
This is where and how i call this method;
connectNotificationService(id: any) {
let stompClient = this.webSocketNotification.connect();
stompClient.connect({},
frame => {
stompClient.subscribe('/topic/notification/' + id, bildirim => {
this.bildirim = JSON.parse(bildirim.body);
this.notificationListesi.push(this.bildirim);
this.unreadNotifications++;
});
}
);
}
At first, project was working correctly but now it started create double connection. When i debug my code i see insede connect function run twice even i call just one time.
This is the picture of console

As you can see connection open sequential. How can i solve this issue ?
You can try either of the following:
- Please update one of the samples and see if that works for you.
- Share a version of your project so that I can examine the issue.
@kum-deepak This is the all code for Web Socket connection.
var SockJs = require('sockjs-client');
var Stomp = require('stompjs');
@Injectable()
export class WebSocketNotificationService {
connect() {
let socket = new SockJs(`http://localhost:8084/socket`);
return Stomp.over(socket);
}
}
`
export class JhiMainComponent implements OnDestroy, OnInit {
notification : Notification;
notificationList: Array<Notification> = [];
unreadNotifications = 0;
constructor( private webSocketNotification: WebSocketNotificationService) {}
ngOnInit() {
this.connectNotificationService(12);
}
connectNotificationService(id: any) {
let stompClient = this.webSocketNotification.connect();
stompClient.connect({},
frame => {
stompClient.subscribe('/topic/notification/' + id, notification => {
this.notification = JSON.parse(notification .body);
this.notificationList.push(this.notification);
this.unreadNotifications++;
});
}
);
}}`
This is all i got. I also want to mention that sometimes just create 1 connection not 2. But its randomly.
Unfortunately it is not sufficient for me to provide any solution.
It seems you are using Angular, if yes, please check https://stomp-js.github.io/guide/ng2-stompjs/ng2-stomp-with-angular7.html. It discusses ng2-stompjs a specialized wrapper of stompjs which exposes Angular services in RxJS interfaces. There is a sample as well, you can start from there.
@kum-deepak Thank u so much it resolved.