My question is, does this code increase server load? how do i get data from the database every few seconds. if it is harmful, then what is the best way to do the same.
Of course, this increases the load. For each socket.io connection that is created on your server, you start a separate timer with an interval of 5 seconds and each time the timer fires, you start one or more database queries and then send these results to this client.
If you had 50 connected clients, you will run these requests every 100 ms (5000/50). If you have 1000 connected clients, the average interval will be every 5 ms. Besides the fact that your database is very busy, your server will also be very busy. If you had thousands of connected clients, scaling quickly degrades.
If you get the same data for each client, you should change the way you do it. Make one query to the database every 5 seconds, and then send the same results to all clients. It is much bigger, much better.
If you don't have to try your database at all and rely on some kind of event triggers to detect changes, this usually works even better.
source share