Events

IoT.js is based on event-driven programming where objects (called “emitters”) periodically emit named events.

Class: EventEmitter

The EventEmitter plays a role as base class for “emitters”. User application would not directly create an instance of EventEmitter since EventEmitter is an abstract trait which defines its behavior and grants to sub-classes.

new EventEmitter()

  • Returns {EventEmitter}.

Returns with a new EventEmitter object.

Example

  1. var EventEmitter = require('events');
  2. var emitter = new EventEmitter();

emitter.addListener(event, listener)

  • event {string} The name of the event.
  • listener {Function} The callback function.
    • args {any}.
  • Returns emitter {EventEmitter}.

It is an alias for emitter.on(eventName, listener).

Adds the listener callback function to the end of the listener’s list for the given event. No checks are made to see if the listener has already been added. In case of multiple calls the listener will be added and called multiple times.

Example

  1. var EventEmitter = require('events');
  2. var emitter = new EventEmitter();
  3. var eventSequence = '';
  4. var listener1 = function() {
  5. eventSequence += '2';
  6. };
  7. emitter.addListener('test', listener1);
  8. emitter.addListener('test', listener1);
  9. emitter.emit('test');
  10. console.log(eventSequence); // prints '22'

emitter.on(event, listener)

  • event {string} The name of the event.
  • listener {Function} The callback function.
    • args {any}.
  • Returns emitter {EventEmitter}.

Adds the listener callback function to the end of the listener’s list for the given event. No checks are made to see if the listener has already been added. In case of multiple calls the listener will be added and called multiple times.

Example

  1. var EventEmitter = require('events');
  2. var emitter = new EventEmitter();
  3. emitter.on('event', function() {
  4. console.log('emit event');
  5. });
  6. emitter.emit('event');

emitter.emit(event[, args..])

  • event {string} The name of the event.
    • args {any}.
  • Returns {boolean}.

Synchronously calls each of the listeners registered for the event, in the order they were registered, passing the supplied arguments to each.

Returns true if the event had listeners, false otherwise.

Example

  1. var EventEmitter = require('events');
  2. var emitter = new EventEmitter();
  3. emitter.addListener('event', function() {
  4. console.log('emit event');
  5. });
  6. emitter.emit('event'); // true
  7. emitter.emit('not_an_event'); // false

emitter.once(event, listener)

  • event {string} The name of the event.
  • listener {Function} The callback function.
    • args {any}.
  • Returns emitter {EventEmitter}.

Adds the listener as a one time listener for the event.

Using this method, it is possible to register a listener that is called at most once for a particular event. The listener will be invoked only once, when the first event is emitted.

Example

  1. var EventEmitter = require('events');
  2. var assert = require('assert');
  3. var emitter = new EventEmitter();
  4. var onceCnt = 0;
  5. emitter.once('once', function() {
  6. onceCnt += 1;
  7. });
  8. assert.equal(onceCnt, 0);
  9. emitter.emit('once');
  10. assert.equal(onceCnt, 1);
  11. emitter.emit('once');
  12. assert.equal(onceCnt, 1);

emitter.removeListener(event, listener)

  • event {string} The name of the event.
  • listener {Function} The callback function.
    • args {any}.
  • Returns emitter {EventEmitter}.

Removes listener from the list of event listeners.

If you add the same listener multiple times, this removes only one instance of them.

Example

  1. var EventEmitter = require('events');
  2. var emitter = new EventEmitter();
  3. var listener = function() {
  4. console.log('listener');
  5. };
  6. emitter.addListener('event', listener);
  7. emitter.removeListener('event', listener);

emitter.removeAllListeners([event])

  • event {string} The name of the event.
  • Returns emitter {EventEmitter}.

Removes all listeners.

If event was specified, it only removes the listeners for that event.

Example

  1. var EventEmitter = require('events');
  2. var emitter = new EventEmitter();
  3. function removableListener() {
  4. console.log("listener called");
  5. }
  6. emitter.addListener('event1', removableListener);
  7. emitter.addListener('event2', removableListener);
  8. emitter.addListener('event2', removableListener);
  9. emitter.addListener('event3', removableListener);
  10. emitter.removeAllListeners('event2');
  11. var res = emitter.emit('event2'); // res == false
  12. res = emitter.emit('event1'); // res == true, prints "listener called"
  13. emitter.removeAllListeners();
  14. res = emitter.emit('event1'); // res == false
  15. res = emitter.emit('event3'); // res == false