Ember.evented subscriptions and notifications - Developers’ Notes

iRonin IT Team - Experts in software development
developers notes, ember

Want a shortcut for subscriptions and notifying users about messages? We’ve found a way. Ember has built-in code for implementing the pub-sub pattern.

Today’s refactoring of a small part of our Ember app, responsible for connecting to PubNub to provide real-time updates, resulted in a discovery. We have a dedicated pubnub service to wrap up PubNub communication. For subscribing and notifying all interested objects about new PubNub messages, we use the pub-sub pattern. For implementation, we relied on custom, simple functions inside the service:

import Ember from 'ember';
const { Service } = Ember;

export default Service.extend({
  addListener(listener) {

  removeListener(listener) {

  // Then to notify listeners
  this.get('listeners').forEach((listener) => {

It turns out that similar functionality is already built-in inside Ember thanks to the Ember.Evented module. They only have different names: on / off instead of addListener and removeListener. But the idea is the same. Refactoring the pubnub service boiled down to removing addListener, removeListener and extending Ember.Evented:

import Ember from 'ember';
const { Evented, Service } = Ember;

export default Service.extend(Evented, {
  // Then to notify listeners
  this.trigger('receive', response.message);

We also had to replace our listener code for subscribing to events (simple search & replace within the project):

// Before

// After
this.get('pubnub').on('receive', callback);
this.get('pubnub').off('receive', callback);

Thanks to that, we decreased the number of custom code inside the service and leveraged Ember’s power. Ember.evented also allows subscribers to be notified about a single event thanks to one, and also implements the has method to check whether there are any subscribers for a specific event. We are also able to use the same implementation for notifications about different events, i.e. disconnect, connect, etc.

Today’s Developers’ Notes were taken straight from the journal of Łukasz, our Web Application Development Team Leader. Keep an eye out for more programming tips and tricks in the future!

Author's Bio
iRonin IT Team

Experts in software development

We are a 100% remote team of software development experts, providing web & mobile application development and DevOps services for international clients.

Similar articles

Bulletproof your development with remote team augmentation

Read how
This page is best viewed in portrait mode
Our websites and web services use cookies. We use cookies and collected data to enhance your experience, provide additional communication channels, improve marketing materials and enhance our offer. IRONIN SP. Z O.O. SP. K. is committed to protecting all the data that we collect or process in any way, especially data of personal nature. By accepting these terms you agree to our usage of cookies and processing your data, according to our Privacy Policy, and you declare that your browser settings reflect your preferences. Read more You have the right to revoke this agreement at any time, based on the terms of our Privacy Policy. You can change cookies settings in your browser. If you do not agree with us using cookies and processing your data, please change your cookies settings in your web browser and reject these terms. You can find more information about cookies, your data privacy This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies. data processing, and your rights in our Privacy Policy.