node-http-proxy load balance websocket error - node.js

I have just started evaluating the node-http-proxy because I need to have a scalable web socket server.
I have tested the ’simple-balancer-with-websockets’ example provided in the repository but it does not work when acting as a proxy to multiple addresses. It only works as a proxy for one address!
When proxying to multiple addresses a WebSocket hangup error as follows:
Error: socket hang up
at createHangUpError (http.js:1472:15)
at Socket.socketOnEnd [as onend] (http.js:1568:23)
at Socket.g (events.js:180:16)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
I am using:
node 0.10.26
socket io 1.0.6
node-http-proxy 1.1.5
platform OSX
The following is the load-balancer. Its only difference to the provided sample is the addresses used and the listen port.
var http = require('http'),
httpProxy = require('http-proxy');
//
// A simple round-robin load balancing strategy.
//
// First, list the servers you want to use in your rotation.
//
var addresses = [
{
host: 'localhost',
port: 8000
},
{
host: 'localhost',
port: 8001
},
{
host: 'localhost',
port: 8002
}
];
//
// Create a HttpProxy object for each target
//
var proxies = addresses.map(function (target) {
return new httpProxy.createProxyServer({
target: target
});
});
//
// Get the proxy at the front of the array, put it at the end and return it
// If you want a fancier balancer, put your code here
//
function nextProxy() {
var proxy = proxies.shift();
proxies.push(proxy);
return proxy;
}
//
// Get the 'next' proxy and send the http request
//
var server = http.createServer(function (req, res) {
nextProxy().web(req, res);
});
//
// Get the 'next' proxy and send the upgrade request
//
server.on('upgrade', function (req, socket, head) {
nextProxy().ws(req, socket, head);
});
server.listen(9000);
The basic http server acting as a target for the above load-balancer is:
var http = require('http'),
fs = require('fs'),
io = require('socket.io');
var args = process.argv.splice(2);
var port = args[0] || 8000;
server = http.createServer(function(req, res) {
var filePath = (__dirname + '/public/connect.html');
fs.readFile(filePath,function (err, data){
res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
res.write(data);
res.end();
});
});
server.listen(port, function() {
console.log('ws listening on: ' + port);
});
io = io(server);
io.on('connect', function(socket){
console.log('socket connected');
socket.emit('message', 'ws message from ' + port);
});
The client html is:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
socket.on('message', function (data) {
console.log(data);
});
</script>
</head>
<body>
node-http-proxy basic load balance test with websockets
</body>
</html>
I regard this as a basic test and yet it does not work! Can anyone explain what I am doing wrong and suggest a solution please?
Many thanks for any thoughts.

Socket.io 1.0 needs sticky sessions. See socket.io/docs/using-multiple-nodes
First engine.io makes an xhr request then it makes an websocket request. Both requests need to reach the same socket.io server. Even more so if engine.io needs to fallback to long polling etc . . .
To fix it, you just need to make your proxy server session aware. It can still round robin fresh connections, but as soon as it serves a socket.io request it needs to route subsequent requests from that session to the same backend.

Related

Chat application using socket.io not connecting when deploying on production server

I am trying to implement chat application using nodejs and socket.io. The application works on localhost. But when I deploy same on my production server then socket.io can't make any connection.
Code for server.js
var express = require('express');
var app = express();
var socket = require('socket.io');
var chat_controller = require('./controllers/ChatController.js');
var user_controller = require('./controllers/UserController.js');
var Group_controller = require('./controllers/GroupChatController.js');
app.get('/search', function (req, res) {
user_controller.get(req, res);
});
app.get('/groupSearch', function (req, res) {
user_controller.get(req, res);
});
var server = app.listen(3600, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
});
var io = socket(server);
io.on('connection', (socket) => {
console.log('made socket connection', socket.id);
socket.broadcast.emit('userconnected');
chat_controller.respond(io, socket);
Group_controller.respond(io, socket);
user_controller.respond(io, socket);
});
io.on('disconnect', function () {
console.log('made socket disconnect', socket.id);
});
Code for client.js
var socket = io.connect('https://www.mywebsite.com', {
path: '/apichat'
});
/* Other events related to socket. */
As my server uses SSL I can't used IP:PORT directly so I am using ProxyPass as
ProxyPass /apichat http://127.0.0.1:3600
After all this still socket connection is not established between server and client.
Error shown in browser console is:
POST https://www.mywebsite.com/apichat/?EIO=3&transport=polling&t=MUc-TJK 404 (Not Found)
And in browser Network tab it shows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /</pre>
</body>
</html>
I have checked many other questions posted here and other sites but no question address this issue.
Please Help.
The issue you are encountering is probably due to ssl enabled on your website.
You need to pass ssl related files in your app.js file. Sample code for this is as follow:
var fs = require('fs');
var options = {
key: fs.readFileSync('PATH_TO_SSL_KEYS.key'),
cert: fs.readFileSync('PATH_TO_SSL_CERTS.crt'),
ca: fs.readFileSync('PATH_TO_SSL.pem')
};
var app = require('https').createServer(options, handler), io = require('socket.io').listen(app);
io.set('transports', [
'websocket',
'flashsocket',
'htmlfile',
'xhr-polling',
'jsonp-polling',
'polling'
]);
function handler(req, res) {
res.writeHead(200);
res.end("welcome sir!");
}
var chat_controller = require('./controllers/ChatController.js');
var user_controller = require('./controllers/UserController.js');
io.sockets.on('connection', function (socket) {
socket.broadcast.emit('userconnected');
chat_controller.respond(io, socket);
user_controller.respond(io, socket);
socket.on('message', function (data) {
socket.broadcast.emit('message', data);
});
});
io.on('disconnect', function (socket) {
console.log('made socket disconnect', socket.id);
});
app.listen(3300);
Try editing your application file as per above mentioned sample code and then try to use it. If you can't get path to ssl related file, then you need to contact either your system administrator or the hosting provider.
I hope it helped.

Node proxy web sockets how to check

I use the following module and it works fine for reverse proxy
https://github.com/nodejitsu/node-http-proxy
currently I've used the code like the following example
httpProxy.createServer({
target: 'ws://localhost:9014',
ws: true
}).listen(8014);
my question is how can I check/simulate that the websockets are working?
Any test will be helpful...
In response to the OP's request for browser test, I modified my original solution to proxy both HTTP and WS traffic to a server where an index.html file is served. This file then connects the browser to the proxy server via WebSocket, which the proxy then proxies to the main server. A simple message is printed on the browser document from the main server.
So that there is no need to copy/paste anything, I created this repo with full instruction: https://github.com/caasjj/httpproxy.git
Here is the code in case others want to look at it here. To run the whole thing, create the two server files and the index.html file, start the servers with node proxyreceiver.js and node proxyserver.js and then navigate to localhost:8014/index.html.
(proxyserver.js):
var httpProxy = require('http-proxy');
var http = require('http');
var proxy = new httpProxy.createProxyServer({
target: {
host: 'localhost',
port: 9014
}
});
var proxyServer = http.createServer(function (req, res) {
proxy.web(req, res);
});
//
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
proxyServer.on('upgrade', function (req, socket, head) {
proxy.ws(req, socket, head);
});
proxyServer.listen(8014);
(proxyreceiver.js):
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(9014);
function handler (req, res) {
res.writeHead(200);
fs.readFile('index.html', function(err, data){
res.end(data);
})
}
io.on('connection', function (socket) {
socket.emit('data', { message: 'Hello World!' });
socket.on('resp', function(msg) {
console.log('Got message: ', msg);
});
});
(index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Web Socket Proxy Test</title>
<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
<script>
var socket = io('http://localhost:8014');
var p = document.createElement("p")
socket.on('data', function (data) {
console.log('Got', data);
p.innerHTML = "Received:" + data.message;
document.body.appendChild(p);
});
</script>
</head>
<body>
<h1>Test ProxyServer</h1>
</body>
</html>
The best way to test is to create a client to connect to it.
there are many ws modules around. Or you can use this: https://www.websocket.org/echo.html just put your url there and test it.

Using net module in socket.io browser keeps loading

I want to use the tcp net module in node.js, my clients will be browser and also not browser ( device ).
when I tried to run in the browser the index.html, my browser keeps loading looks like it looping..I dont know what's wrong in my code.
I tried use telnet it works fine, the problem is on the browser i cannot load properly the index.html
//app.js
var net = require('net');
var io = require('socket.io')(net);
var clients = [];
var server = net.createServer(function (socket) {
console.log("New client connected");
clients.push(socket);
});
server.listen(1337, 'localhost', false, function () {
console.log('server bound');
});
io.on('connection',function(socket){
socket.emit('news', { hello: 'world' });
});
here is my client code.
http://pastie.org/10115599
Both the browser and socket.io require an http server, not just a TCP server. You can see the code examples in the socket.io documentation. The first server and client code example on that doc page shows you the basics you need.
In fact, the first step in socket.io connection is an http request that is then "upgraded" to the webSocket protocol. So, the server must be an http server. And socket.io hooks into an http server in order to receive incoming connections.
Here's a code example from the socket.io doc:
Server Code:
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
app.listen(80);
io.on('connection', function (socket) {
// incoming socket.io connection established
});
function handler (req, res) {
// process http requests for normal web page serving
}
Client Code:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>

socket.io-client doesn't connect

I am trying to connect two node.js servers using socket.io and socket.io-client. In both cases, I am using v0.9.16.
In my case, I call the servers the STREAM SERVER and the ACTIVITY SERVER where the activity server uses the socket.io-client module.
STREAM SERVER - (THE CODE ON THE SERVER THAT ACTS LIKE A SERVER)
var https = require('https');
var express = require('express');
var socket = require('socket.io');
var securePort = (process.env.LOCAL_HTTPS_PORT || 443);
var sslOptions = //CERTIFICATE
var socketIoConfiguration = //CONFIG VALUES
var app = express();
var server = https.createServer(sslOptions, app);
var io = socket.listen(server, socketIoConfiguration('activityToStream'));
io.sockets.on('connection', function (socket) {
console.log('Activity server connected to stream server.');
});
server.listen(securePort);
ACTIVITY SERVER - (THE CODE ON THE SERVER THAT ACTS LIKE THE CLIENT)
var socketClientModule = require('socket.io-client');
var streamConnectionServer = 'https://165.225.144.273:443';
var activityToStreamSocket = socketClientModule.connect(streamConnectionServer);
activityToStreamSocket.on('connect', function(socket){
console.log('Connected to Stream Server');
});
When I run this code, I don't get any message from either server. However, when I run this code from an HTML page served in Chrome, I see messages on the output of the Stream server:
<!DOCTYPE html>
<html>
<head>
<title>
Test
</title>
<script src="https://165.225.144.273/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('https://165.225.144.273');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</head>
<body>
Hello World
</body>
</html>
STREAM SERVER MESSAGES WHEN CLIENT RUN IN CHROME
debug: client authorized
info: handshake authorized UaUDRsA3ZBTgdsiLDCrl
debug: setting request GET /socket.io/1/websocket/UaUDRsA3ZBTgdsiLDCrl
debug: set heartbeat interval for client UaUDRsA3ZBTgdsiLDCrl
debug: client authorized for
debug: websocket writing 1::
So, it makes me think the issue is with my "ACTIVITY SERVER" running socket.io-client, but I cannot figure out why its not working at all (no error messages, etc.)
Use the same version of socket server and client
I had the same issue, but my solution was simple:
My code was wrong here:
var serverSocket = ioclient.connect('127.0.0.1:6000');
Then I wrote 'ws://' before '127.0.0.1', and it began to work.
var serverSocket = ioclient.connect('ws://127.0.0.1:6000');
That gave me a headache for hours.
I was able to get it to work using the following:
1) socket.io v 1.0.0-pre using this command
npm install git+https://github.com/LearnBoost/socket.io.git
2) scoket.io-client v 1.0.0-pre using this command
npm install git+https://github.com/LearnBoost/socket.io-client.git
3) I used an SSL certificate from a CA instead of self signed and then I used a DNS name instead of an IP address (see this on not using unsigned certificates with Socket.io)
Here's my server and client (server acting as client). Reference the Socket.io-client GitHub page.
SERVER CODE
var sslOptions = //CERTIFICATE
var app = require('express')(); // Express 3.x
var server = require('https').Server(sslOptions, app);
var io = require('socket.io')(server);
io.on('connection', function(socket) {
console.log('Connection from Activity Server');
for (var i=0; i<100000; i++) {
io.emit('event', i);
}
});
server.listen(443);
CLIENT CODE (SERVER ACTING AS CLIENT)
var now = Date.now();
var socket = require('socket.io-client')('https://example.com:443');
socket.on('connect', function(){
console.log('Connected to Stream Server');
socket.on('event', function(data){
console.log('Ping ' + data);
console.log(now + ' ' + Date.now());
});
});
Using this Socket.io code, I was able to get over 13,000 messages per second. This was done on two Joyent servers in the same data center (Memory 256 MB, CPUs 0.125 and bursting, Network Up to 10 Gbit/s).
When doing more of a "Ping Pong" style messaging, I was able to get about 650 messages per second.
Note the performance numbers are just a datapoint for reference, I didn't do any tweaking, heaving loading, etc.
I got the same situation but I fix this by switching the socketio-client version. I got socketio version 2.3. but I got socketio-client version 3.1. I changed the version of socketio client by using "npm install socket.io-client#2.3". in my client js file I use socket.io-client like that
const io = require('socket.io-client');
const socket = io('ws://localhost:3000',{
});

Connect to node.js socket.io server: clients don't connect - server reports" destroying non-socket.io upgrade"

I thought that socket.io would allow me to implement a websocket server. I have this very simple code:
// Require HTTP module (to start server) and Socket.IO
var http = require('http'), io = require('socket.io');
// start at port 8888
var server = http.createServer(function(req, res) {
res.writeHead(200,{'Content-Type': 'text\html'});
res.end('<h1>Welcome to the Notification Server</h1>');
});
server.listen(8888);
// Create Socket.io obj/pass to server
var socket = io.listen(server);
socket.on('connection', function(client) {
console.log('Successful Websocket connection!');
client.on('message', function(event) {
console.log("Received message from client", event);
});
client.on('disconnect', function() {
console.log('Client has disconnected');
});
});
I've tried a few different test clients all of which generate this message on the server: debug - destroying non-socket.io upgrade
One such client attempt has some code like this:
<html>
<script type="text/javascript">
<!---
window.WebSocket = window.WebSocket || window.MozWebSocket;
var ws = new WebSocket("ws://dev.ourserver.com:8888");
ws.onopen = function() {
alert("Connected");
}
ws.onerror = function(error) {
alert("Error:"+error);
}
// -->
</script>
<body>
</body>
</html>
As soon as we load the page I get the debug message on the server.
I thought the point of this library was to support the websocket protocol and any client supporting websockets would be able to connect.
If I interpret the message literally it seems to indicate that server.io has detected that it is connecting to a "non socket.io" client. Does this mean that there is no way for me to connect to this server without using socket.io in the client?

Resources