O'Reilly logo

Socket.IO Cookbook by Tyson Cadenhead

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a server-side clock

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.

How to do it…

To create a server-side clock that emits data to the client-side, follow these steps:

  1. First, create a server.js file that emits a new date string every second. To do this, we will set an interval of 1000 ms and emit new Date() for this interval. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required