Creating a real-time widget
The configuration options and well thought out methods of socket.io
make for a highly versatile library. Let's explore the dexterity of socket.io
by creating a real-time widget that can be placed on any website and instantly interfacing it with a remote Socket.IO server. We're doing this to begin providing a constantly updated total of all users currently on the site. We'll name it the live online counter (loc
for short).
Our widget is for public consumption and should require only basic knowledge, so we want a very simple interface. Loading our widget through a script
tag and then initializing the widget with a prefabricated init
method would be ideal (this allows us to predefine properties before initialization if necessary).
Getting ready
We'll need to create a new folder with some new files: widget_server.js
, widget_client.js
, server.js
, and index.html
.
How to do it...
Let's create the index.html
file to define the kind of interface we want as follows:
<html...