stompjs icon indicating copy to clipboard operation
stompjs copied to clipboard

Stompjs create sequential double connection

Open buseodaci opened this issue 6 years ago • 4 comments

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 image

As you can see connection open sequential. How can i solve this issue ?

buseodaci avatar Mar 23 '20 11:03 buseodaci

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 avatar Mar 24 '20 13:03 kum-deepak

@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.

buseodaci avatar Mar 25 '20 08:03 buseodaci

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 avatar Mar 27 '20 12:03 kum-deepak

@kum-deepak Thank u so much it resolved.

buseodaci avatar Mar 30 '20 06:03 buseodaci