Skip to main content

Soket.io Use Of Ping Pong Events | node-js-tutorial.blogspot.in






When we Create Client Server Application,
Some time we Need To Check out Live Connectivity between Client & Server.

Let's we Understand by Example,

Server Code.


 const fs       = require('fs');  
 const http      = require('http');  
 const path      = require('path');  
 const SocketIOServer = require('socket.io');  
 const app = new http.Server();  
 app.on('request', (req, res) => {  
  const index = path.join(__dirname, 'public', 'index.html')  
  fs.readFile(index, function (err, data) {  
   if (err) {  
    res.writeHead(500);  
    return res.end('Error loading index.html');  
   }  
   res.writeHead(200);  
   res.end(data);  
  });  
 });  
 app.listen(3000, 'localhost');  
 const io = new SocketIOServer(app);  
 // could also use "connection"  
 io.on('connect', function (socket) {  
  console.log(`${socket.id} "connect"`);  
  socket.on('ping', (data) => {  
   console.log('Receive "Ping Form Client"');  
   io.emit('pong', {});   
   console.log('Send "Pong To Client"');   
  });  
  socket.on('disconnect', () => {  
   console.log(`${socket.id} "disconnect"`);    
  });  
 });  

When Server Recive Ping event , then he send Cilent Pong Event.

Client Side Code.

 <!DOCTYPE html>  
 <html lang="en">  
  <head>  
   <meta charset="utf-8">  
   <meta http-equiv="X-UA-Compatible" content="IE=edge">  
   <meta name="viewport" content="width=device-width, initial-scale=1">  
   <title>Socket.IO Events</title>  
  </head>  
  <body>  
   <h1>Socket.IO Events</h1>  
   <script src="/socket.io/socket.io.js"></script>  
   <script>  
    const socket = io();  
    socket.on('pong', (data) => {  
     console.log('Receive "pong Form Server"');  
    });  
    socket.emit('ping', {});  
    console.log('Send "ping To Client"');  
   </script>  
  </body>  
 </html>  


Here, Client Send Ping Event To Server. and server Replay With Pong Methods.

Just Copy & Past Above code for Test Ping Pong.

Thanks.
NodeJs Tutorial

Comments

Post a Comment

Popular posts from this blog

NodeJs - Socket.IO Set Ping Timeout & Ping Interval | node-js-tutorial.blogspot.in

How to Socket.io Specifying Heartbeat Here we Learn Method of Set Ping Timeout & ping interval. There Are Several Way to Set Ping interval & Ping Time out. Please Remember Some note : ping interval is alway small then ping Timeout. Always Set Same ping timeout & ping Interval for both side. (Client side  & Server Side) When You Develop any Client Server Application. Following Are some Pattern Which most Case Peopele use. 1) Type One  var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http, { 'pingInterval': 2000, 'pingTimeout': 5000 }); 2) Type Two For package dependency versions: "express": "^4.12.3", "socket.io": "1.0" ex : var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); io.set('heartbeat timeout'

How To Get Socket(socket.io) Disconnected Reason - Node js | JavaScript

we Define Event handler for Handel "Disconnect" Event. Just Get Reason we use Following Code. socket.on('disconnect', function(reason){    console.log('Player Disconnected Due To Reason :'+reason); }); Start listening for server-sent events from node with the specified eventIdentity. Will trigger the provided callback function when a matching event is received. Here 'disconnect' Event Occure when some socket is disconnected. There Are Following Type of Reason Here we Get. 1) Transport Close 2) Ping Timeout Transport Close : When You Close your Application/Game/Webgl, You May Get this Reason. Ping Timeout : When you ping time is Expire ,  You May Get this Reason. It might be better to set different pingTimeouts on the server and on client. On server, pingTimeout (+pingInterval) means the idle time the server will keep the socket up. This can be quite long, as the client can sleep while the connection is still kept al