EIS Game Dev
Nov 27, 2015
Reaction score
First Language
Primarily Uses
Events are the backbone of Node.js. Node.js is an event-based API that uses events to manage complex operations. So, what are events?

Before we can talk about Node.js events, the best place to start is with JavaScript events.

JavaScript events -- clicking the mouse button, typing into a text field are prime examples of how the Node.js event API works. You register a function to an event, and when that event occurs the code is executed. This is powerful and offers a new layer of control over any program we write as developers. So, let's talk about Node.js events.

Node.js Events
Node.js events occur like JavaScript events as stated before. And, you have the option of creating your own events, or your own event-based class. Now, Node.js events are all within the "events" module; this module contains the EventEmitter class.

The EventEmitter class can be used as is or can be extended with your own code. The class has three important methods for controlling events; the methods are: on, once, and emit. Methods on and once register functions to that event name; emit triggers the functions related to that event name in the order they were added. Furthermore, you can pass any parameter type to the emit method for your register functions to use. If you're working with games or even used to regular programming, events may not seem useful. But, effective use of events can make your code cleaner. So, when would you use events?

Practical Example Of Event Use
Events are a clean way to handle states within a class or for creating links between different classes in your code. For example, you may have a class called car and tracker. When the car starts, you want to track the car. You could have all the cars call a function on the tracker to pass an instance of themselves to the tracker. Or, you could emit and event from the car that passes that information to the tracker. Using events can improve the code base and makes it easier to understand with a small event-based solution to getting information between classes that shouldn't directly reference each other. Now, here's an example of creating a state machine using events:

Here's the ES5 Version:

Here's the console output:


As you can see we define a couple events with on and once methods. We use our codeColor method to emit the initialize and changeState events. Because initialize is registered to a once method, it will only appear once as "Initial State: Blue Ocean". The changeState event emits the changedState event, logging the state of our stateMachine when called. Using the emit method, we can have this executed anywhere in our code base without calling the registered function directly. If we wanted, we could have made StateMachine inherit from EventEmitter, and as events are emitted, it changes its internal state. One example of this is the Stream class in Node.js. A stream has different events emitted based on changes within its internal state such as receiving data or when receiving data has ended and more. Anyway, let's look at the output of the code above.

With that said, I hope this post helped you understand the event module in Node.js. It's the backbone of the tool, and understanding it will help you understand how other modules and classes work in many JavaScript libraries for Node.js.

Latest Threads

Latest Profile Posts

just beat ff7 original for the first time since 2010. having played remake last year, I have a much better appreciation for the original oddly enough. I still like some of the character presentation in remake more, but the original's got a lot going for it.
Ever reach that moment in your life when your juggling a dozen or more projects that you know will never be made but don't have the heart to delete them? Ugh, I hate those moments...
:kaosigh: Why must you hurt me so?
my main character isn't the warrior of light or the chosen one, he's a self righteous, invasive try hard who gets ahead of himself and makes a critical error, resulting in a downward moral spiral that, eventually, leads him to truths that make him a better man, and im DAMN proud of that.
Generate Actors, Dark Fantasy Monsters, & I Heard There Was a Chosen One, | RPG Maker News #42

Work on Cat Quest: The Last Catmancer continues slowly but surely. :kaopride: Today I received the Game Over screen from @Finnuval!

Forum statistics

Latest member