JavaScript event listening example(s)

Hi all,

Could anyone point me to some JavaScript event handling example(s) or snippet(s)? This is what I would like to achieve through a module:

  • Load my JS code upon loading my module.
  • Listen/hook to JS global events (such as a ‘visit started’ event).

Note that this is not in the context of a specific page or fragment, I would like the above to ‘just happen’ in the client-side background, so to say. I guess I am hoping that there is some sort of global JS event bus available to listen to.

Any directions, examples, advice would be much welcome!

There is no global JS event bus, nor is there a global event feed on the server side in openmrs-core. But it would be really cool if there was!

There’s an atomfeed module (not used anywhere as far as I know) and also some Bahmni atom feed functionality.

Brainstorming, what are you envisioning? That the client page would subscribe to “things about patient X” so if the patient has a new visit started, it reloads data?

Hi @darius, thanks for the prompt response.

It’s rather “things about all the patients on an active visit”: we want to cache locally the treatment plans and the medication orders for those on an active visit. So for a start we need to capture when visits start and end.

And if we wanted to go down the route of starting to develop a JS event bus, where would this effort start?

Alternatively could you point us to another direction to JS-capture events? At least as a temporary workaround until a full on event bus would be available.

Offhand, I would use the Event module to listen on create & update of Encounter and Visit, and use this to create an a feed of “changes to patients with an active visit” (which you’d expose via REST.

You could also look at the module that Bahmni is using for atom feed purposes and see if you can leverage this. See

I think I get the idea, but let’s simplify it for the sake of the example.

Imagine that my REST service returns the list of patients currently on an active visit. What is the need of the Event Module? Each time the request is sent, the response is filled with the current list of patients on an active visit, full stop.

And what would you think of using EventSource with Server-sent events on the client? In our case that would work as it is really a one way communication server > client. I am not familiar with Atom feeds though.

The reason I mentioned event and atomfeed is that I was presuming you’d want to be notified when something new happens to any of the patients with active visits, so that you can publish just this, and the client doesn’t blindly keep downloading all the data for all patients with active visits.

Do you mean that it would work like this:

  • The feed class listens to events.
  • The feed updates its content based on events.
  • The REST service builds its response from the feed’s content.

So the feed is available to the REST service for providing just what it needs?

Then, on the client-side, I could use SSE (Server-sent events) in my JS to then do whatever is needed with the encapsulated data.

You need to listen to events, but then you have two possible approaches:

a. Build up a server side view of your content; serve this to the client as requested (decrease server load to handle the same number of client requests). Presumably in this case the client requests the entire state periodically.

b. Publish a feed of relevant events, and by monitoring this feed the client knows when it actually needs to reload state (decrease the number of expensive client requests). Presumably in this case the client requests content patient-by-patient as patients are updated.

(You could do one or both of these. When I said atom feed I was thinking of the second approach.)

1 Like

At the moment, everyone is finding their own way. Sharing & learning about each other’s approaches can help avoid re-inventing the wheel.

As we identify the most common needs (e.g., subscribing to patient change event), I’d think this would be the type of thing we’d want an OpenMRS object from an openmrs.js (via bower install openmrs) would provide. I’d expect that OpenMRS would use something like to provide method(s) for binding to shallow changes on the server.

1 Like

Thanks both @burke and @darius for the suggestions. We need to estimate the extent of the effort to get this done and I think we have now a much better view.

We will start work on this within two weeks to a month, and I hope that soon after that I will be able to come back to this thread to point people to the work done on our repo.


Has there been any movement on this idea? I’m working with the MOTECH team who originally commissioned the Atom Feed and we now have a requirement to get it working again. I see the work done by ThoughtWorks in the links, but wonder if an internal bus has been considered.

Thanks, Craig

Hi @craigappl,

Despite saying that we would start work on this, we have not started yet at all. Making OpenMRS work offline was pushed down on our priority list for now. However this subject may come back on the table within the next few months.

Do you mean on the client-side, like a JS event bus?