Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected behaviour for repeated subscribe/unsubscribe #342

Open
jkburges opened this issue Aug 1, 2023 · 0 comments
Open

Expected behaviour for repeated subscribe/unsubscribe #342

jkburges opened this issue Aug 1, 2023 · 0 comments

Comments

@jkburges
Copy link

jkburges commented Aug 1, 2023

I am trying to do a drop-in replacement of an existing but slow synchronous-API call with an async workflow, using Sidekiq on the backend and Message Bus front and back.

The existing front-end code looks roughly like:

getResults(params) {
  return callApi(params); // responds 
}

My attempt to replace it looks like:

lastMessageId = -2;

async getResults(params) {
  const asyncResponsePromise = new Promise((resolve) => {
    // MessageBus.stop(); MessageBus.start(); // uncomment and things work.
    Message.subscribe("thechannel", (message, globalId, messageId) => {
      lastMessageId = messageId;
      Message.unsubscribe("thechannel");
      resolve(message)
    }, lastMessageId);
  });

  callApiAsync(params); // server responds immediately but triggers a background job which will be handled by Sidekiq.

  return await asyncResponsePromise;
}

The problem I am having is that getResults() is called repeatedly (user scrolling down to get more results); the first time is fine and the results are returned as soon as the backend publishes the message. Subsequent calls though don't return as soon as the message is published but rather, after the next poll starts (roughly 25s).

I see that when you unsubscribe the current long poll is aborted if there is one (https://github.com/discourse/message_bus/blob/main/assets/message-bus.js#L561). Why is that?

And if I stop/start message bus just before subscribing, that seems to work around the problem. But I wanted to know if I'm misusing the MessageBus by repeatedly subscribing/unsubscribing like this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant