Before reading this article, please go through the following articles:
-
HTML5 WebSockets Introduction: Part 1
-
HTML5 WebSockets(Test WebSocket For Client/server): Part 2
After learning about HTML5 WebSockets and testing with a client-server in my previous article, here in this article we will test the WebSockets for broadcasting messages.
What is broadcasting messages? Broadcasting messages are like when you want to send a message to you all the clients at the same time, like in a chatting interface you chat with a single person while in broadcasting the message you send will be delivered to all the clients at the same time. To test HTML5 WebSockets for broadcasting messages, please read my previous article because some steps are the same as for my previous article.
Step 1: For the server-side, first download and install the node.js.
Step 2: Now install socket.io as done in the previous article.
Step 3: Now to create the js file under in the location C:\Program Files\nodejs as you did previously.
Write the following code and save the file as whatever name you want with the .js extension, I used "serverfile.js" here.
- var http = require('http');
- var io = require('socket.io');
-
- var yourserver = http.createServer(function (request, response) {
- response.writeHead(250, { 'Content-Type': 'text/html' });
- response.end('Your WebSocket server is running');
- }).listen(22222);
-
- var yoursocket = io.listen(yourserver).set('log', 1);
-
- yoursocket.on('connection', function (client) {
- client.on('YourcustomMessage', function (data) {
- console.log('Client Custom Message: ', data);
-
- var current = new Date().getTime();
-
- client.broadcast.emit('YourMessageResponse', data + '(broadcasted)->' + current);
- });
- client.on('disconnect', function () {
- console.log('Your Client disconnected');
- });
- });
As you can see the difference between the code above and my previous article serverfile.js code, this js code I create for broadcasting the message.
Step 4: Now start the server by again going to the command prompt and run the following command
That's it; your server is started successfully, as shown in the command prompt.
Now to prepare the main client-side.
For the client-side preparation, first, you need to create an HTML file for the interface to the client and do something on the page. So, the HTML page looks as in the following:
- <!DOCTYPE html>
- <html>
- <head>
- <script src='http://localhost:22222/socket.io/socket.io.js'></script>
- </head>
- <body>
- <script>
- var HTML5WebSockets = {};
- HTML5WebSockets.socketio = {
- yoursocket: null,
-
- initialize: function () {
-
- HTML5WebSockets.socketio.yoursocket = io.connect('http://localhost:22222');
-
- HTML5WebSockets.socketio.yoursocket.on('connect', function () {
- HTML5WebSockets.socketio.log('You are connected to Server<br />');
- });
-
- HTML5WebSockets.socketio.yoursocket.on('YourMessageResponse', function (data) {
- HTML5WebSockets.socketio.log('Server Custom Response: ' + data + '<br />');
- });
-
- HTML5WebSockets.socketio.yoursocket.on('disconnect', function () {
- HTML5WebSockets.socketio.log('You are disconnected from Server<br />');
- });
-
- document.querySelector('#sendCustMes').onclick = function () {
- HTML5WebSockets.socketio.emitCustomMessageToServer(document.querySelector('#custMes').value);
- document.querySelector('#custMes').value = '';
- };
- },
-
- emitCustomMessageToServer: function (data) {
- HTML5WebSockets.socketio.yoursocket.emit('YourcustomMessage', data);
- HTML5WebSockets.socketio.log('Custom message to Server: ' + data + '<br />');
- },
-
- log: function (message) {
- document.querySelector('#log').innerHTML += message;
- }
- }
- </script>
-
- <div id='log'></div>
- <input type='text' id='custMes' />
- <button type='button' id='sendCustMes'>Brodcast</button>
-
- <script>
- HTML5WebSockets.socketio.initialize();
- </script>
- </body>
- </html>
Good. You are ready to broadcast a message. Do one thing here open the same HTML in 3 or 4, as many as you want to test the broadcast message.
Now if your server is started successfully and you run you HTML page then you will see a message on-page that you are connected to the server as in the following image
Now just type your message and hit the send button. You will immediately see a message below the server connected message that your message will be broadcast and then go to your server or command prompt, there you will see how your message will reach the server and how it will reach all the browser windows.
Go and check your broadcast message on the server