Open In App

What is poll phase in Node.js event loop ?

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In the Node.js event loop, the poll phase is a time when the event loop handles events from the poll queue. The poll queue contains events that are ready to be handled, such as new connections that are waiting to be accepted or data that has been received from a network socket.

During the poll phase, the event loop will iterate through the events in the poll queue and hand them off to the appropriate event handlers for processing. If the poll queue is empty, the event loop will block and wait for new events to be added to the queue.

After the poll phase is completed, the event loop will move on to the check phase, where it will handle any setImmediate() callbacks that are ready to be executed. The event loop will then return to the poll phase to handle any new events that have been added to the queue.

The poll phase of the Node.js event loop has two main functions:

1. Determining how long to block and poll for I/O events: When the event loop enters the poll phase, it will block and wait for new events to be added to the poll queue. The length of time that the event loop blocks and waits for new events are determined by the value of the poll parameter in the setTimeout() function. If the value of the poll is -1, the event loop will block indefinitely until new events are available. If the value of the poll is a positive number, the event loop will block for that number of milliseconds before moving on to the next phase of the event loop.

2. Processing events in the poll queue: After the event loop has determined how long to block and poll for new events, it will iterate through the events in the poll queue and hand them off to the appropriate event handlers for processing. This is where most of the JavaScript code that you have written will be executed, starting at the top and working its way down through the code.

If there are no timers scheduled when the event loop reaches the poll phase, one of two things may happen:

  • If the poll queue is not empty: the event loop will iterate through the queue, running the callbacks in the queue synchronously until the queue is exhausted or the system-dependent hard limit is reached.
  • If setImmediate() was not used to schedule scripts: the event loop will wait for callbacks to be added to the queue before executing them.

What is Long Polling? Let me explain in three simple points:

1. Long polling is a technique used to establish a stable connection between a client and a server without using protocols such as WebSocket or Server-Sent Events. In long polling, the client sends a request to the server, and the server responds only when it has new and specific information. Until that time, the connection remains open.

2. The client can then send another request as soon as the server responds, and the server will return a response when data is available. This process begins again when the client closes the connection and the server sends a request.

3. In Node.js, long polling can be implemented by keeping the connection between the client and server open until the server has complete data to respond with. While the connection is open, the client is waiting for the server to respond with data before sending another request.

Long polling can be configured on the client side by creating an event loop that sends requests to the server during periods of downtime or when making new requests.

Example: This example code demonstrates this whole polling  process:

Javascript




const net = require('net');
  
const server = net.createServer(socket => {
    // This function is the event handler 
    // for incoming connections.
      
    // It will be called for each new 
    //connection that is received.
  
    console.log('New connection received:', socket.remoteAddress);
    // ... handle requests from the client here ...
});
  
server.listen(3000, () => {
    console.log('Server listening on port 3000');
});


Output:

Server listening on port 3000
New connection received: [remote address]

In this example, the call to net.createServer() creates a new server and registers an event handler function to be called for each incoming connection. When the server is started using the listen() method, it begins listening for incoming connections on port 3000. When a new connection is received, the event handler function is called and passed a socket object representing the connection. This event is added to the poll queue, and the event loop will handle it during the poll phase.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads