What is Event-Driven Programming.

What is Event-Driven Programming?

The Event-Driven Programming is an Architectural design pattern to build the software application where components of the runtime open event and responds to the event. In this article, we will discuss the traditional Programming and Event-Driven Programming with detailed comparison and examples to understand.

Traditional Programming

Let’s see how HTTP 1.0 to understand how events are handled in Traditional Programming.

Traditional Programming
Traditional Programming

Let’s assume you are clicking a button in the browser and it is sending a request to the server. Then the server receives the request. Then it will process the request as per the programmed function or method. Then it will send back the processed response to the browser. In this, the server will close the connection once after sending the response back to the browser. If Browser needs another response, it should open a new request and get a response.

Event-Driven Programming

Let’s assume the same scenario in Event-Driven Programming. Like normally, when the user clicks the button in the browser, it will send a request to the server. The Server process the input as per the function or method. Then, the Server send the processed response to the browser. Unlike traditional Programming, In Event-Driven programming will not close the connection and still receive the data. It will not be closed unless we manually close the connection.

Event-Driven Programming
Event-Driven Programming

So, The Server will emit an Event and the Client will send a request and get a response by events. Let’s discuss in more detail about the Event-Driven Programming with example.

How Event-Driven Programming works.

As we discussed, In Event-Driven Programming, we collect events and send them to Event-Handler via Scheduler. This Scheduler will remain open until it is closed by the counter-event. Just like any other program. the Scheduler will have the list of events consumed and depends on the instructions, it will assign it the respective handler or if the event cannot be assigned, it will throw an exception.

How Event-Driven Programming works.
How Event-Driven Programming works.

In Event-Driven Programming, Central Event-handler is commonly called as dispatcher and scheduler. This Architectural design is always compared with the Publish-Subscribe Approach.

Let’s see the terms used in Event-Driven Programming

  • Event Object
  • Event Source
  • Event Listener

Event Object

Since it is built on Object-oriented programming, Events are packed as an object when it gets created. We call that as the Event Object.

Event Source

It is the object that triggered the event. For example, A click event of the button in the browser or timed notification or message.

Event Listener

It is the piece of code in the runtime which is open and listen for the event.

Let’s see an example of the Event-Driven program in Node.js.

In Node.js, we have a concept called Event Loop which is called non-blocking I/O operation. First, event Emitter will send the event to Event Queue which will keep the list of events that should be handled in order. Then it will be sent to an event loop that will keep the connection open then according to the program of the event handler, it will respond to the output. This is called Event Lifecycle.

Event Listener
Event Listener

In programming, we have the following Node.js snippet which will be the part of this Event-Loop which will help us understand more about Event-Driven Programming.

In the above snippet,

var events = require('events');

This line will Import the Event Module to the code.

With the imported Event module, we must create an object of Event.

var eventEmitter = new events.EventEmitter();

Then, create a function that will act as the event handler.

var messageHandler = function message() {
   console.log('Message sent succesful.');
   eventEmitter.emit('massage_received');
}

Now, we must bind the event with the handler.

eventEmitter.on('messages', messageHandler);

Let’s create another function to bind with the event.

eventEmitter.on('massage_received', function() {
   console.log('Message received succesfully.');
});

Finally, let’s emit the event.

eventEmitter.emit('messages');

This way an event will be captured and handled with event handler in the event loop.

Conclusion

This way Event-Driven Programming works and the Event-Driven Architecture is taking a step ahead of any other design patter in terms of performance. In our upcoming article, we will discuss more programming design patterns and use them. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps and App Development.

1 thought on “What is Event-Driven Programming?”

  1. Thank you so much share this type of informative blog they will share all about event management it will be easy for us through parties and all to keep sharing like this. Good luck!

Leave a Reply