question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onConnect-Callback

See original GitHub issue

Hey guys,

is it possible to wrap the activate() function in a Promise on your side? Otherwise I guess this client is not that useful … I am trying to use/execute it in a lambda function …

My workaround atm is:

  async connect(): Promise<void> { // Todo!
    this.client.activate();
    while (!this.client.connected) {
      await new Promise(resolve => setTimeout(resolve, 50));
    }
  }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Feb 22, 2021

So, this is my current solution:

  async connect(): Promise<void> {
    if (!this.stompjs.connected) {
      return new Promise((resolve, reject) => {
        this.stompjs.activate();
        this.stompjs.onConnect = () => {
          resolve();
        }
        // Todo: onError?
      });
    }
  }

  async send(
    queueName: string,
    body:      T,
  ): Promise<void> {
    try {
      await this.connect();
      const receiptId = nanoid();
      return new Promise((resolve, reject) => {
        this.stompjs.publish({ destination: queueName, body: JSON.stringify(body), headers: { receipt: receiptId } });
        this.stompjs.watchForReceipt(receiptId, () => {
          resolve();
        })
        // Todo: onError?
      });
    } catch(e) {
      console.error("RabbitMQ failed: %s", e);
      throw e;
    }
  }
  
//  ...
await send("queue", { key: "value" });

Maybe this is helpful for others facing this issue.

0reactions
kum-deepakcommented, Feb 16, 2021

I think that should work.

Based on your requirements, you may be able to write similar code, though a different structure, without the RxJS wrapper.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mqttclient.onconnect(callback) - Dev Center - Electric Imp
This method allows you to register a function that will be called whenever an attempt to connect to an MQTT broker (see mqttclient.connect())...
Read more >
Filebeat -> elastic: onConnect callback failed: error loading ...
I have followed the instructions here: Grant privileges and roles needed for publishing | Filebeat Reference [7.17] | Elastic I have ...
Read more >
`onConnect` callback support · Discussion #407 - GitHub
It would be really nice to have support for an onConnect callback which gets fired after the user connects their wallet. wagmi already...
Read more >
Issue with onConnect callback function - Question - Mbed
Issue with onConnect callback function. This is how I am setting the onConnection call back in my code. ble.gap().onConnection(aConnectionCallback);.
Read more >
OnConnect() Callback is not getting called using mosquitto C
mosquitto_connect() function is executing successfully but onConnect() function is not getting called for some unknown reason. onDisconnect() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found