Node.js Tips Part 3

junior az
5 min readSep 9, 2020

For the previous part of the Node’s Tips, we talked about the importance of knowing the basics of Node.js to be able to start a Node application. we talked about how to print out a message on your terminal using the Console.log or using the Global methods.

In this section, we will discuss the other valuable tips that should be known to any novice so let us dive straight into it. Now that we’ve covered the first four steps we‘re going to skip the next thing right ahead.

5- Events

we are going to talk about how do events work in Node.js. Often people describe it as an asynchronous event-driven JavaScript runtime, the runtime implements a thing called an event loop just like a web browser does and it allows Node to push intensive operations off to a separate thread so that only very fast non-blocking operations happen on the main thread and this is why people often call Node non-blocking.

It is a design choice that makes Node.js very suitable for things that require high throughputs like real-time applications, web servers, and things like that. So the question is how does that affect the coder?

Well normally the coders do not have to think about the low-level details, otherwise, they just need to know how events and callbacks work. In most cases, coders listen to events that can come in many forms but one example is on the process Global that we looked at before.

Before the Node process finishes it emits an event named exit, we can listen to this event using On and then register a callback function as the second argument, when the exit event occurs Node.js will run this function and that is where it gets the name callback because the function is not called the first time Node.js sees it and it is only called after exit event occurs at some point in the future.

This event is built-in to Node but we can also create our own from scratch, we will have to import an event emitter from the events module that is built into Node and we will look at modules in more detail later.

We can create a custom event emitter by instantiating the class and then we register a callback that fires on the lunch event and now that the callback is registered we can simply call the event emitter to emit with that event name and that triggers our callback function to run.

As you can see in the above image we emit the event twice which will run the call back function twice.

this event-driven style of programming is extremely useful when u have something that is CPU intensive. The Node Events module lets you emit and control events. In Node lots of built-in modules (objects) have the ability to emit or broadcast an event. For eg, each time the data is read from the source, the fs or File source module emits an event called ‘data.’ In Node terms, the artifacts emitting events are referred to as event emitters.

The event emitter system models on Observer pattern based on Java where an event observer is informed every time the event occurs. This article will also show you how to emit and handle events using Node.js event API.

Two types of entities are dealt with in the Node event handling framework: event emitter and the event listener. Here functions are specifically divided. For a specific event, the event listener registers with the event emitter and listens to that event. It is important to remember that event listeners can be multiple i.e. for a single event, more than one listener can be recorded. When you emit an event, an event form is ultimately emitted which is described as a string. As you can see from the code above, ‘press’ is the type of event that is described as a string. Event names are specified by the user, and one may choose the event name based on an object‘s behavior or state. To use Node event handling, you must install and use the module Events. The code below uses the Events module to display the basic handling of events.

Passing parameter to the callback

Even the event can be managed by passing one or more parameters to the callback function. You define what data is to be passed on to the callback function when emitting an occurrence. The code below shows the same:

You transfer the value ‘world’ in the code above while emitting the event. It is then treated by passing it as an argument in the callback function.

The following functions are also provided by the Node Event Emitter API:

addListener() — To add an event listener to a particular event
once() — To add an event listener to a particular event that will be invoked at most once
removeEventListener() — Removes a specific event listener for a specified event
removeAllListeners() — Removes all event listeners for a specified event.

Using addListener

With addListener the method you can bind a named callback function. The below code shows how a named callback is used with addListener() the method.

In the above code, we are using sayHello a named function. Though you can still use the inline anonymous callback function with addListener() the method, just like the on() method.

That was brief about event handing in Node. With various Node modules, you can play around and review the API doc to see what events they emit and manage them accordingly.

In this article, we covered a few examples and tricks on how to use Node.js, and in the next article we will talk about other important steps to use when building an application with Node.js, and with that, we can move on to the next step.

--

--