Too many EIO socket.io polling calls slowing the web page - node.js

My application is based on Socket.io to have a chat functionality.
My application is deployed on red hat open shift.
I keep getting http://url/socket.io/?EIO=3&transport=polling&t=Lj8huKr&sid=y1OB9OBmdSd_Ma4nAAFG requests which are huge in number.
And they are probably also blocking the loading of my web page which is slowing my web page.
I read in the internet that this error comes if the port is not mentioned. But my port number is coming of red hat open shift configuration page.
Below is the code of how socket.io is intialized in my initial html page the node js initial server page
index.html
var socket =io();
index.js (node js server)
var app = express();
var server = app.listen(process.env.NODE_PORT || 3006, process.env.NODE_IP || 'localhost', function(){
console.log('listening on port 3006');
});
var io = require('socket.io').listen(server);
io.on('connection', function(socket) {
console.log('a user connected');
socket.on('disconnect', function() {
console.log('user disconnected');
});
socket.on('chat message', function(msg) {
console.log(msg);
io.emit('chat message', msg);
});

In socket.io-client v1.x, the initial connection will start as polling so you will always see at least some requests to the http://site.url/socket.io/?EIO=3&transport=polling URL.
Socket.io clients will then attempt to upgrade to a websocket connection. Those clients that can't upgrade will remain polling regularly to get as close to real time as they can. If you have a large number of clients that are polling every 2 seconds then you will see a lot of requests.
v2.x+ clients moved to using websockets first, but will fallback to the same long polling a URL if the websocket connection fails.

Related

How to test maximum connection Node JS websocket

I'm writing an Node JS socket App.
Case1: Base on this link
stackabuse.com/node-js-websocket-examples-with-socket-io/
I start the server on port 3000, go to browser, navigate to localhost:3000, i got index.html page and an connection to socket inside this file (you can find it in the link above).
This is my code
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
console.log(current);
});
io.on('connection', function(socket){
//console.log('a user connected');
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
socket.on('disconnect', function(){
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Case 2: I include socket.io.js file inside index.html file, then access it from browser (but not with port 3000 anymore) with apache like an normal site).
Code for Node server case 2
Then the script will create an websocket to localhost:3000 like this:
Client case 2
I only can test the maximum connect with case 1 (actually send alot GET request to get "index.html" page, not an real socket connection). I tested with thor, loadtest, benchmark, Artillery... and they're only work with case 1 :(
So, can I have any tool to test maximum connection in Websocket Node JS? I really need an usable tool to test websocket (not just HTTP GET).
Thansk for reading this question, and sorry for my English, by the way :(

socket.io app hangs in production environment

I am using socket.io based chat application.
I have established an eventing mechanism between client and server. It works fine in the development.
But when i deploy the same code to production and multiple users start using it. After some time of use the application hangs. Node.js server stops responding.
I found that when the user is disconnected he can not connect back again.
My server side code
app.listen(3006);
console.log('listening on port 3006');
//socket io event listening
var io = require('socket.io').listen(server);
io.on('connection', function(socket) {
console.log('a user connected');
socket.on('disconnect', function() {
console.log('user disconnected');
});
socket.on('chat message', function(msg) {
console.log(msg);
io.emit('chat message', msg);
});
Below is the client code
<script src="/socket.io/socket.io.js"></script>
<script>
var socket =io();
i am on socket io 1.4.5 and using express with node.js
I think the tcp connections opened by web sockets are opened, reaches the max limit and any new connection is refused.
does client have to perform any thing on the disconnect socket.io event to release the tcp conncetion ?
Best Regards,
Saurav

Create web socket streams between a server and arbitrary NodeJS processes

Having a server running on localhost:5000, I want to connect to that server from another NodeJS process, via web sockets.
From my experience with web sockets, I always needed the server object to create a web socket server.
var http = require('http');
// create http server
var server = http.createServer(function(req, res) {
// serve files and responses
...
});
// Socket.io server listens to our app
var io = require('socket.io').listen(server);
// Send current time to all connected clients
function sendTime() {
io.sockets.emit('time', { time: new Date().toJSON() });
}
// Send current time every 10 secs
setInterval(sendTime, 10000);
// Emit welcome message on connection
io.sockets.on('connection', function(socket) {
socket.emit('welcome', { message: 'Welcome!' });
socket.on('i am client', console.log);
});
server.listen(3000);
This is a tiny example using socket.io. Without having access to get the server variable (since this server will be deployed some where in the cloud), how can I connect via web sockets to this server?
An ugly solution would be via HTTP requests, but that's not web sockets. I want to keep the connection open and pipe data there.
How can I do that?
You get the socket.io-client module, require() it into your other nodejs server and use that client module from your other server (which will be the client in this case) and connect from that server to this one.
Example code here: https://github.com/automattic/socket.io-client
var socket = require('socket.io-client')('http://localhost');
socket.on('connect', function(){});
socket.on('event', function(data){});
socket.on('disconnect', function(){});

Server showing multiple client connection (net nodejs)

I am trying to make a server which listens on a internet facing port and forwards incoming http requests to an internal express server listening at another port. Following is the relevant part of the code I'm using.
var net = require('net');
var server = net.createServer();
server.listen(addr.from[3], addr.from[2], function(){
console.log('Server listening');
});
server.on('connection',function(from){
console.log('Client connected from '+ from.remoteAddress);
var to = net.createConnection({
host: addr.to[2],
port: addr.to[3]
});
from.pipe(to);
to.pipe(from);
from.on('error',function(err){
winston.error('Error at unix box'+err);
to.end();
});
to.on('error',function(err){
winston.error('Error at middleware server'+err);
from.end();
});
from.on('end',function(){
console.log('Client disconnected ');
to.end();
});
to.on('end',function(){
console.log('Middleware disconnected');
from.end();
});
});
The problem I'm encountering is that, when I open "ip:port" in the browser (which would be the internet facing port) I'm getting messages multiple "client connected from xxxxxx" msgs on the console. Can anyone help me understand why this is happening?
Whenever browser connects to a website it usually makes two requests: normal and to retrieve favicon.
Funny thing, is that the favicon request is not even displayed in browser developer tools.
To verify, you need to extract the request made, print it to server, and then observe why you get multiple requests. For that, connection might be too early, try hooking request event instead:
server.on('request', funtion(req, res) { console.log(req.url); });

Socket.io fires connect event just after the second connect

I'm using socket.io with the latest version of node.js and socket.io shows an curious behavior:
The connect event fires on client side but not on server side for the first time.
After a reload or simply loading the page again the event got fired on both - client and server - side correctly.
What's wrong (with my code?)?
// Client
var socket = io.connect(window.location.protocol + '//' + window.location.hostname, {
'sync disconnect on unload': true
});
socket.on('connect', function() {
alert("Connect");
// Do other stuff
});
-
// Server
io.sockets.on('connection', function(socket) {
console.log("connection");
io.sockets.on('connect', function(socket) {
console.log("connect");
});
});
Started the server and loaded the page:
Client fires the alert, server just logs connection. Now loading the page again, it logs both connection and connect.
Update:
It seems that just the very first connection has such issues, afterwards it works everywhere as expected. Just after every node server (re)start, that behavior appears.
Note: node itself delivers the page where the socket.io is used and that works even on the first request, so a node issue should be excluded then.
Browser is also doesn't matter, it's the same on every browser.
For Socket.IO, connection is the server-side equivalent of connect on the client side. Therefore, when you're inside the callback for the connection event, the socket has already established a connection and you don't need to listen on some other event. I'm not sure what connect on the server side is, but it is not documented and does not work.
// on the server
io.sockets.on('connection', function(socket) {
// this connection has already been established
// no other handler is required
});
// on the client
var socket = io.connect();
socket.on('connect', function() {
// the connection has been established
});

Resources