How to test maximum connection Node JS websocket - node.js

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 :(

Related

Is it good that Socket.io listens the same port as HTTP does?

My question is when WebSocket and HTTP listen the same port, will that be a problem? Will WebSocket slow down the HTTP server or vice versa?
No, it shouldn't cause a problem. They need to run on the same port, but the port doesn't have to explicitly be port 80 (which is the default HTTP port). Whatever your Node HTTP server listens on, Socket.io listens to as well. The code below is from the Socket.io website. As you can see, both the server and Socket.io listen on port 3000.
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');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
This shouldn't cause a problem, because after all it is required. The only other option would be to incorporate an I-frame or allow cross-origin requests. But depending on what kind of Socket.io app and how many connections are concurrently connected determines its efficiency. So it may or may not slow the server down. Unless you have millions of connections and emits, speed probably shouldn't be an issue.

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

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.

Socket IO net::ERR_CONNECTION_REFUSED

I am trying to implement socket.io into my application which is hosted at Azurewebsites. webapp
Here is the server.js
var app = require('express')();
var server = require('http').createServer(app);
server.listen(process.env.PORT || 3001)
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
var io = require('socket.io')(server);
io.on('connection', function (socket) {
console.log("Socket connected :"+socket.id);
socket.emit('news', { hello: 'world' });
});
And Here is the client side socket. index.html
<script src="socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:3001');
console.log("scoekt connect",socket)
socket.on('connect', function(){ console.log('connected to socket'); });
socket.on('error', function(e){ console.log('error' + e); });
socket.on( 'news', function( data ){
console.log("socket data",data);
});</script>
I am getting the below error
I am not really sure whats is going wrong. Here is the structure of file-ing system
ROOT
app/
index.html
server.js
web.config
PS: this is an Angular2 application
PS: I have checked all the suggested question based on this error but none solved my issue, thus i am posting this question.
Per my experience, Azure Web App doesn't bind loaclhost or 127.0.0.1 to your website, and only ports 80 and 443 are public-facing. This maps to a specific port for your app to listen to, retrievable via process.env.PORT. So you'd need to replace
var socket = io('http://localhost:3001');
with
var socket = io('http://<your app name>.azurewebsites.net');
And if your server side and client side in the different domain, you'd also need to enable CORS on the server side. In Azure, we can enable it with the Azure portal.
In a browser, go to the Azure portal, and navigate to your App Service.
Click CORS in the API menu.
Enter each URL in the empty Allowed Origins text box. A new text box is created. As an alternative, you can enter an asterisk (*) to specify that all origin domains are accepted.
Click Save.
Socket.IO uses WebSockets, which are not enabled by default on Azure. You can also enable WebSocket support using the Azure Portal. Please see the steps below.
In the Azure portal, click Application settings in the SETTINGS menu.
Under Web Sockets click On
Click Save.
For more info, please refer to this documentation.

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); });

Resources