Long Polling Message Bus Notifications from React Application

Hi all,

I’m currently building a React frontend to a headless Discourse server.

I am in the process of implementing the notifications count, similar to the one over the users avatar on here. I’d like this to be updated in near real-time using long polling over Discourse’s message_bus.

I have seen the message-bus.js file but it is very verbose and out of date for a modern application. I don’t really want to be bringing in jQuery and a global object when we should be doing it as part of the react application.

I have also seen in /initializers/subscribe-user-notifications.js.es6 that we subscribe to /notification/userid but I cannot find documentation for the channels available to subscribe to or what the responses are.

I’m looking for help as to the best-practice way to implement subscribing to message bus in a modern javascript application.

Thanks! :+1:

2 Likes

Discourse is a modern javascript application :stuck_out_tongue:

The best way is to use the message-bus library. Currently it does depend on JQuery, but if you would like to submit a pull request to remove that dependency, I’m sure it would be considered.

Messagebus is only intended to be used within Discourse, it’s not really a ‘public’ API. The best source of information will be the javascript files in the codebase, which you already found :+1:

Minor correction :slight_smile:

MessageBus will first attempt to use jQuery and then fallback to a plain XMLHttpRequest version that’s contained in the message-bus-ajax.js file. message-bus-ajax.js must be loaded after message-bus.js for it to be used. You may override this option with a function that implements an ajax request by some other means

https://github.com/discourse/message_bus

3 Likes

Thanks @david, @sam, I didn’t mean to suggest that Discourse isn’t a modern application - it obviously is. I think I found message-bus.js first and I was confused whether that was truly the best-practice. It works right so its not a problem.

Thanks!

3 Likes

Discourse core uses the exact same file, it is the right file ™

1 Like