discourse / discourse/message_bus

Expected behaviour for repeated subscribe/unsubscribe

Open
#342 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
1.7k
Forks
129
Avg merge
40m
Merged PRs (30d)
1

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with assets/message-bus.js around line 561 and trace how unsubscribe interacts with the active long poll. Reproduce repeated getResults() calls using the subscribe/unsubscribe pattern, then determine whether the behavior is expected and document why the next poll is delayed or what usage guidance is needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, ruby
Domain
backend, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.