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

How do we acknowledge message publish using watchForReceipt? #122

Closed zymr-keshav closed 5 years ago

zymr-keshav commented 5 years ago

What I want to achieve is once I ( FE ) confirm that WebSocket handshake has been established then want to send a message to the server and here comes a method in RxStomp watchForReceipt and I am using that method but a bit confused how does server acknowledge this? does Server need to do something for acknowledging or it will automatically be acknowledged?

we have Java spring boot on BE side and I am sending this way

 sendMessage() {
        const message = { command: this.command.GET_USER_LIST };
        const headers = Object.assign(this.headers, { receipt: '161718 });
        this.rxStompService.publish({
            destination: `/message/${this.uniqueID}`,
            body: JSON.stringify(message),
            headers: headers
        });
    }

and write this one in another place

 this.rxStompService.watchForReceipt('161718', function(frame: Frame) {
            console.log('%c Watch for receipt', 'color: green');
            console.log({ frame });
        });

BUT this is never getting logged.

Am I missing something or Server also need to do logic to acknowledge this request?

kum-deepak commented 5 years ago

You must add the call to watchForReceipt before your publish call. Otherwise the receipt may be missed.

zymr-keshav commented 5 years ago

I am doing this way but unable to receive any:

 this.rxStompService.watchForReceipt('161718', function(frame: Frame) {
            console.log('%c Watch for receipt', 'color: green');
            console.log({ frame });
        });
        const message = { command: this.command.GEt_USER_LIST };
        const headers = Object.assign(this.headers, { receipt: '161718' }); 
        this.rxStompService.publish({
            destination: `/message/${this.uniqueID}`,
            body: JSON.stringify(message),
            headers: headers
        });
kum-deepak commented 5 years ago

Which broker are you using? Does your broker support receipts?

zymr-keshav commented 5 years ago

STOMP

kum-deepak commented 5 years ago

STOMP is the protocol. I wanted to know - is it RabbitMQ, Spring Boot, Active MQ or any other?

zymr-keshav commented 5 years ago

its' Spring Boot

kum-deepak commented 5 years ago

I have tested the following code with RabbitMQ as broker:

    const message = `Message generated at ${new Date}`;
    const receipt_id = UUID.UUID();
    this.rxStompService.watchForReceipt(receipt_id, (frame) => {
      console.log('Receipt: ', frame);
    });
    this.rxStompService.publish({destination: '/topic/demo', body: message, headers: {receipt: receipt_id}});

I can confirm it receive the receipts. Please see the change-set at https://github.com/stomp-js/ng2-stompjs-angular7/commit/9c70530b84f6de75c2b27225c21c0a42b9902098. Full code at https://github.com/stomp-js/ng2-stompjs-angular7 - branch publish-acknowledgements.

It may be worth checking whether Spring Boot supports receipts for message publishes.

kum-deepak commented 5 years ago

Closing because lack of activity.