It is sometimes useful to have the date and time from the server side instead of the client side. We're usually interested in the client-side time zone, but this isn't always the case. For example, if we create a dashboard for a web-hosting platform, it could be helpful to display the time as it appears on the server.
In this recipe, we will create a server-side clock that updates the user interface in real time so that we always know what time the server thinks it is.
To create a server-side clock that emits data to the client-side, follow these steps:
First, create a
server.js
file that emits a new date string every second. To do this, we will set an interval of1000
ms and emitnew Date()
for this interval. In JavaScript, when we createnew Date()
with no arguments, it will always be set to the current date and time, as shown in the following code:var express = require('express'), app = express(), http = require('http'), socketIO = require...