I have a service where clients subscribe to a socket connection and listens for updates coming down the pipeline. Once a client has subscribed to a room we send a sendSyncRequest (view below) which calls another service to grab any updated data and it publishes it back down the client through the socket room.
The issue that keeps popping comes when the socket.io version was updated from v1.7.3 to v2.1.1. After deploying the service to a production server everything goes a bit haywire and we start seeing a getaddrinfo ENOTFOUND error that pops up everytime sendSyncRequest gets hit.
Some additional information:
OS Version: Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-74-generic x86_64)
Node Version: 4.4.0
Socket.io Version: 2.2.1
Request Version: 2.81.0
Error screenshot:
getadrrinfo ENOTFOUND error
sendSyncRequest Code:
function sendSyncRequest(service, accessToken, lastUpdate, socketId, method) {
var uri = service.scheme + '://' + service.host + ':' + service.port + service.path;
var options = {
uri: uri,
strictSSL: false,
timeout: NO_RESPONSE_TIMEOUT,
json: true,
headers: {
Authorization: 'Bearer ' + accessToken
}
};
if (method === 'get') {
options.qs = {
lastUpdate: lastUpdate,
socketId: socketId
};
} else {
options.body = {
lastUpdate: lastUpdate,
socketId: socketId
};
}
request[method || 'post'](options, function(err, response) {
if (err) {
log.error('sendSyncRequest', err);
return;
}
if (response.statusCode !== 200) {
log.error('sendSyncRequest %d %s', response.statusCode, http.STATUS_CODES[response.statusCode]);
return;
}
}
);}
Socket Server Code:
module.exports.init = function (callback) {
log.info('starting new Socket.IO server');
io.origins('*:*');
log.info('initializing socket.io server');
io.attach(server.getServer());
io.on('disconnect', function (socket) {
log.info('socket.io server disconnect ', socket);
});
io.on('connection', function (socket) {
log.info('new socket.io client %s#%s (%s) connection',
socket);
socket.emit('successful_connection', {socketId: socket.id});
socket.on('disconnecting', function () {
log.info('socket.io client %s#%s (%s) disconnecting',
socket.userData.user,
socket.userData.domain,
socket.id);
for (var room in socket.rooms) {
if (socket.rooms.hasOwnProperty(room)) {
var count = io.sockets.adapter.rooms[room].length;
log.debug('current room %s connected client count = %d', room, count);
if (count <= 1) {
log.info('unsubscribing from room %s', room);
cacheClient.unsubscribe(room);
}
}
}
});
});
};
We have also looked into the following github issues which talks about the file descriptor limits, but ultimately this feels like a short term solution.
https://github.com/nodejs/node-v0.x-archive/issues/5545#issuecomment-19007785
https://github.com/nodejs/node-v0.x-archive/issues/5488
If anyone has any suggestions or fixes, please share, we welcome all the help we can get.
Related
I have two backends. Backend A and Backend B.
Backend B sends and receives info using a socket server running at port 4243.
Then, with Backend A, I need to catch that info and save it. But I have to also have a socket server on Backend A running at port 4243.
The problem is that, when I run Backend A after running Backend B I receive the error "EADDRINUSE", because I'm using the same host:port on both apps.
If, for Backend A I use Python, the problem dissapear because I have a configuration for sockets that's called SO_REUSEADDR.
Here we have some examples:
https://www.programcreek.com/python/example/410/socket.SO_REUSEADDR
https://subscription.packtpub.com/book/networking-and-servers/9781849513463/1/ch01lvl1sec18/reusing-socket-addresses
But, I want to use JavaScript for coding my Backend A, so I was using the net package for coding the sockets, and I can't get it to work, because of the "EADDRINUSE" error.
The NodeJS documentation says that "All sockets in Node set SO_REUSEADDR already", but it doesn't seem to work for me...
This is my code so far:
// Step 0: Create the netServer and the netClient
console.log(`[DEBUG] Server will listen to: ${HOST}:${PORT}`);
console.log(`[DEBUG] Server will register with: ${AGENT_ID}`);
const netServer = net.createServer((c) => {
console.log('[netServer] Client connected');
c.on('message', (msg) => {
console.log('[netServer] Received `message`, MSG:', msg.toString());
});
c.on('*', (event, msg) => {
console.log('[netServer] Received `*`, EVENT:', event);
console.log('[netServer] Received `*`, MSG:', msg);
});
}).listen({
host: HOST, // 'localhost',
port: PORT, // 4243,
family: 4, // ipv4, same as socket.AF_INET for python
});
// Code copied from nodejs documentation page (doesn't make any difference)
netServer.on('error', function (e) {
if (e.code == 'EADDRINUSE') {
console.log('Address in use, retrying...');
setTimeout(function () {
netServer.close();
netServer.listen(PORT, HOST);
}, 1000);
}
});
const netClient = net.createConnection(PORT, HOST, () => {
console.log('[netClient] Connected');
});
// Step 1: Register to instance B of DTN with agent ID 'bundlesink'
netClient.write(serializeMessage({
messageType: AAPMessageTypes.REGISTER,
eid: AGENT_ID,
}));
With this code, I get the following output in the terminal:
But, with the Python code, the socket connects successfully:
I don't know what to do :(
I hope I get some help here.
Edit 1
By the way, the lsof command, throws me this output for the JavaScript backend:
And this other output for the Python backend:
Edit 2
It really seems to be a problem with JavaScript. I also found this snippet:
var net = require('net');
function startServer(port, host, callback) {
var server = net.createServer();
server.listen(port, host, function() {
callback(undefined, server);
});
server.on('error', function(error) {
console.error('Ah damn!', error);
callback(error);
});
}
startServer(4000, '0.0.0.0', function(error, wildcardServer) {
if (error) return;
startServer(4000, '127.0.0.1', function(error, localhostServer) {
if (error) return;
console.log('Started both servers!');
});
});
From this post:
https://medium.com/#eplawless/node-js-is-a-liar-sometimes-8a28196d56b6
As the author says:
Well, that prints “Started both servers!” which is exactly what we don’t want.
But for me, instead of printing that, I get an error:
Ah damn! Error: listen EADDRINUSE: address already in use 127.0.0.1:4000
at Server.setupListenHandle [as _listen2] (node:net:1319:16)
at listenInCluster (node:net:1367:12)
at doListen (node:net:1505:7)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '127.0.0.1',
port: 4000
}
I really cannot make it to run and print "Started both servers!".
Because that's what I want my code to do.
Edit 3
This is the Python server socket: https://gitlab.com/d3tn/ud3tn/-/blob/master/tools/aap/aap_receive.py
This is the important part:
addr = (args.tcp[0], int(args.tcp[1])) # args.tcp[0] = "localhost", args.tcp[1] = "4243"
with AAPTCPClient(address=addr) as aap_client:
aap_client.register(args.agentid) # args.agentid = "bundlesink"
run_aap_recv(aap_client, args.count, args.verify_pl)
It creates an AAPTCPClient, and the only thing that AAPTCPClient does, is the following:
def __init__(self, socket, address):
self.socket = socket
self.address = address
self.node_eid = None
self.agent_id = None
def register(self, agent_id=None):
"""Attempt to register the specified agent identifier.
Args:
agent_id: The agent identifier to be registered. If None,
uuid.uuid4() is called to generate one.
"""
self.agent_id = agent_id or str(uuid.uuid4())
logger.info(f"Sending REGISTER message for '{agent_id}'...")
msg_ack = self.send(
AAPMessage(AAPMessageType.REGISTER, self.agent_id)
)
assert msg_ack.msg_type == AAPMessageType.ACK
logger.info("ACK message received!")
def send(self, aap_msg):
"""Serialize and send the provided `AAPMessage` to the AAP endpoint.
Args:
aap_msg: The `AAPMessage` to be sent.
"""
self.socket.send(aap_msg.serialize())
return self.receive()
def receive(self):
"""Receive and return the next `AAPMessage`."""
buf = bytearray()
msg = None
while msg is None:
data = self.socket.recv(1)
if not data:
logger.info("Disconnected")
return None
buf += data
try:
msg = AAPMessage.parse(buf)
except InsufficientAAPDataError:
continue
return msg
I don't see any bind, and I don't understand why the python code can call "socket.recv", but in my JavaScript code I can't do "netServer.listen". I think it should be the same.
There are things to clarify.
1.) The client uses the bind syscall where the kernel selects the source port automatically.
It does so by checking sys local_portrange sysctl settings.
1.) If you want to bind the client to a static source port, be sure to select a TCP port outside the local_portrange range !
2.) You cannot subscribe to event "*", instead you've to subscribe to the event "data" to receive messages.
For best practice you should also subscribe to the "error" event in case of errors !
These links will get you started right away:
How do SO_REUSEADDR and SO_REUSEPORT differ?
https://idea.popcount.org/2014-04-03-bind-before-connect/
So, for all beginners, who want to dig deeper into networking using node.js…
A working server example:
// Step 0: Create the netServer and the netClient
//
var HOST = 'localhost';
var PORT = 4243;
var AGENT_ID = 'SO_REUSEADDR DEMO';
var net = require('net');
console.log(`[DEBUG] Server will listen to: ${HOST}:${PORT}`);
console.log(`[DEBUG] Server will register with: ${AGENT_ID}`);
const netServer = net.createServer((c) => {
console.log('[netServer] Client connected');
c.on('data', (msg) => {
console.log('[netServer] Received `message`, MSG:', msg.toString());
});
c.on('end', () => {
console.log('client disconnected');
});
c.on('error', function (e) {
console.log('Error: ' + e.code);
});
c.write('hello\r\n');
c.pipe(c);
}).listen({
host: HOST,
port: PORT,
family: 4, // ipv4, same as socket.AF_INET for python
});
// Code copied from nodejs documentation page (doesn't make any difference)
netServer.on('error', function (e) {
console.log('Error: ' + e.code);
if (e.code == 'EADDRINUSE') {
console.log('Address in use, retrying...');
setTimeout(function () {
netServer.close();
netServer.listen(HOST, PORT);
}, 1000);
}
if ( e.code = 'ECONNRESET' ){
console.log('Connection reset by peer...');
setTimeout(function () {
netServer.close();
netServer.listen(HOST, PORT);
}, 1000);
}
});
The Client:
/* Or use this example tcp client written in node.js. (Originated with
example code from
http://www.hacksparrow.com/tcp-socket-programming-in-node-js.html.) */
var net = require('net');
var HOST = 'localhost';
var PORT = 4243;
var client = new net.Socket();
client.setTimeout(3000);
client.connect(PORT, HOST, function() {
console.log("Connected to " + client.address().address + " Source Port: " + client.address().port + " Family: " + client.address().family);
client.write('Hello, server! Love, Client.');
});
client.on('data', function(data) {
console.log('Received: ' + data);
client.end();
});
client.on('error', function(e) {
console.log('Error: ' + e.code);
});
client.on('timeout', () => {
console.log('socket timeout');
client.end();
});
client.on('close', function() {
console.log('Connection closed');
});
Best Hannes
Steffen Ullrich was completely right.
In my JavaScript code, I was trying to create a server to listen to the port 4243.
But you don't need to have a server in order to listen to some port, you can listen with a client too! (At least that's what I understood)
You can create a client connection as following:
const netClient = net.createConnection(PORT, HOST, () => {
console.log('[netClient] Connected');
});
netClient.on('data', (data) => {
console.log('[netClient] Received data:', data.toString('utf8'));
});
And with "client.on", then you can receive messages as well, as if it were a server.
I hope this is useful to someone else.
I have a working node.js Server that uses a socket.io websocket connection and ssh2 connection to open a shell and return it to a react client in the browser that uses xterm-for-react. I'm now trying to also tunnel a port over the same ssh connection but am having some difficulty. The code is below and the commented out part is where I've been trying to do the port forwarding - equivalent to
ssh -L 2233:192.168.2.1:80 test#192.168.0.65
const socketIO = require("socket.io");
const sshClient = require("ssh2").Client;
const utf8 = require("utf8");
const fs = require("fs");
class SocketServiceFwd {
constructor() {
this.socket = null;
this.pty = null;
this.devicesList = [
{
name: "Device 1",
host: "192.168.0.65",
port: "22",
username: "test",
password: "test12345",
},
];
attachServer(server) {
if (!server) {
throw new Error("Server not found...");
}
const io = socketIO(server, {cors: {origin: "*",},});
console.log("Created socket server. Waiting for client connection.");
// "connection" event happens when any client connects to this io instance.
io.on("connection", (socket) => {
console.log("Client connect to socket.", socket.id);
this.socket = socket;
const ssh = new sshClient();
this.socket.on("devices", (cb) => {
console.log("Get devices list from " + socket.id);
return cb(null,this.devicesList.map((d) => ({ name: d.name }))
);
});
this.socket.on("connectToDevice", (deviceName) => {
console.log(socket.id + " connecting to device " + deviceName);
const selectedDevice = this.devicesList.find(
(d) => d.name === deviceName
);
if (selectedDevice) {
ssh.on("ready", function () {
console.log(socket.id + " successfully connected to " + deviceName);
socket.emit("output", "\r\n*** SSH CONNECTION ESTABLISHED ***\r\n");
// ssh.forwardOut("127.0.0.1", 2233, "192.168.2.1", 80,
// (err, stream) => {
// console.log(
// "ssh forwardOut '127.0.0.1:2233' '192.168.2.1:80'"
// );
// if (err)
// return socket.emit(
// "output",
// "\r\n*** SSH FORWARDOUT ERROR: " + err.message + " ***\r\n"
// );
// }
// );
ssh.shell(function (err, stream) {
if (err)
return socket.emit(
"output",
"\r\n*** SSH SHELL ERROR: " + err.message + " ***\r\n"
);
socket.on("input", function (data) {
stream.write(data);
});
stream.on("data", function (d) {
socket.emit("output", utf8.decode(d.toString("binary")));
});
stream.on("close", function () {
console.log(socket.id + " terminal stream close");
ssh.end();
});
});
});
ssh.on("close", function () {
console.log(socket.id + " ssh connection closed to " + deviceName);
socket.emit("output", "\r\n*** SSH CONNECTION CLOSED ***\r\n");
socket.removeListener("input", () => {});
});
ssh.on("error", function (err) {
console.log(err);
socket.emit("output","\r\n*** SSH CONNECTION ERROR: " + err.message + " ***\r\n");
});
ssh.connect(selectedDevice);
}
});
this.socket.on("disconnectFromDevice", () => {
console.log(socket.id + " disconnecting from device");
ssh.destroy();
});
});
}
}
module.exports = SocketServiceFwd;
***** EDITED *****
It would be great if someone could please provide some advice :-) I have been pushing ahead but still need some assistance...
I have had a little more success using the npm "tunnel-ssh" module. The tunnel does open long enough for me to view the website but shortly after that, the tunnel stops working. Is there something else I'm supposed to be doing after opening the tunnel?
The code below goes where the commented out section above is. I have tried to follow the examples I've seen online - can anyone please advise me what I'm doing wrong?
I'm running DEBUG=* in my npm start and see the following :
tunnel-ssh sshConnection:ready +3s
tunnel-ssh sshConnection:ready +1ms
tunnel-ssh sshStream:create +19ms
tunnel-ssh sshStream:create +0ms
TCP :: ERROR: read ECONNRESET
TCP :: ERROR: read ECONNRESET
So It's clear that the tunnel starts up but then an error occurs. Often the connection recovers and I can continue browsing the website through the tunnel, but eventually an unrecoverable error occurs. So the tunnel is unstable. If i run the tunnel using openssh it is stable. Therefore my question really now boils down to a request for assistance in determining reasons for the instability. Am I not handling something in my code or my configuration correctly? Please Help! Note also, that despite this my shell session continues to work without any problem.
const tunnel = require("tunnel-ssh");
const tnl = tunnel({host: '192.168.0.65', username: 'test', password: 'test12345',
dstHost: '192.168.2.1', dstPort: 80, localHost: '127.0.0.1',
localPort: 2233}, (er, stream) => {
if(er) {
console.log("something went wrong " + er.message);
stream.close();
}
stream.on("data", (data) => {
console.log("TCP :: DATA: " + data);
});
stream.on("error", (error) => {
console.log("TCP :: ERROR: " + error.message);
});
});
I am using a Telnet - client to validate the emails, I connect to the server and it responds with 250 but when I write another command and ask for the answer, it simply does not answer me.
This is my code:
function ConnectTelnet(){
//var connection = new telnet();
var response;
var HOST = 'mail.dominio.com';
var PORT = 25;
var net = require('net');
var client = net.connect(25,'mail.dominio.com',function(){
console.log('connected to server!');
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
client.on('data', function(data) {
console.log('Received: ' + data);
response = data;
if(response.indexOf("220") === -1){
client.write('EHLO dominio.com')
console.log(data)
}
});
})
}
Does anyone know how I can continue? Thanks :)
You can't send the data, get an answer and then send more data on the same connection. TCP does not send messages "separately". TCP is a stream protocol, which means that when you write bytes to the socket, you get the same bytes in the same order at the receiving end. There is no notion of "message boundaries" or "packets" anything of the sort.
If you want to do that, you need to make a new connection every time.
This is how I did to send several EHLO on the same connection:
const net = require('net');
const client = net.createConnection({ host: '127.0.0.1', port: 1025 }, () => {
console.log('connected to server!');
checkEHLO(client, [ 'xxx#xxx.xxx', 'xxx#xxx.xxx', 'xxx#xxx.xxx' ]);
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('disconnected from server');
});
function checkEHLO(client, emails){
emails.forEach((email) => {
client.write('EHLO ' + email + '\n');
});
}
And this was the response I got:
connected to server!
220 127.0.0.1 ESMTP Service Ready
250-Hello xxx#xxx.xxx
250-PIPELINING
250-8BITMIME
250-STARTTLS
250 AUTH PLAIN LOGIN
250-Hello xxx#xxx.xxx
250-PIPELINING
250-8BITMIME
250-STARTTLS
250 AUTH PLAIN LOGIN
250-Hello xxx#xxx.xxx
250-PIPELINING
250-8BITMIME
250-STARTTLS
250 AUTH LOGIN PLAIN
My angularjs client websocketserver can properly send to the server, but when sending from server to client, the client doesn't register the event.
I'm using angular-websockets at the client side and ws at my express.js server
Here's my code.
server
var port = process.env.PORT || 3002;
var server = http.createServer(app); // app = express
server.listen(port);
var socketComs = require('./lib/socketcoms').connect(server);
var connect = function(server) {
var wss = new WebSocketServer({
server: server
});
wss.on('connection', function(ws) {
console.log("websocket connection open");
ws.on('message', function incoming(message) {
console.log('received', message); // THIS WORKS FINE
});
var id = setInterval(function() {
ws.send('pong', 'data 123', function(err) {
console.log('sent pong', err); // THIS IS NEVER CAUGHT BY CLIENT, err = clean
});
}, 2000); // Pong is never received
});
};
client
var connect = function() {
ws.$on('$open', function() {
console.log('wow its working');
ws.$emit('message', 'some message');
});
ws.$on('pong', function(data) {
console.log('yes', data);
});
ws.$on('$close', function(data) {
console.log('wss closed');
});
};
Can anyone see what's going wrong?
I'm using ng-websocket with PHP socket, and I have the same issue.
I just opened the ng-websocket.js and..guess what? the "ping" and "pong" events don't exist!
The "incoming" event is called "$message"...
This is how to get data from server:
ws.$on('$message', function (response) {
console.log("DATA FROM SERVER", response);
With the out of date docs on sails' sockets implementation i'm struggling to get the basic 'connect' message. Here's my node.js server side code:
sails.sockets.on('connection', function(socket){
sails.log.info('socket connected');
//create room and broadcast a welcome message
socket.emit('user joined', {'message': 'Welcome to ' + roomName});
socket.join(roomName);
socket.broadcast.to(roomName).emit('user joined', {'message': 'Welcome to ' + roomName});
});
and my client side:
var sock = io.connect('http://localhost:8888');
sock.on('connection', function(socket){
console.log('conected to server');
});
sock.on('user joined', function (json) {
console.log('socket: ' + json);
});
I do get the OK message from sails itself on start up but can't seem to get a connection of my own:
sails.io.js:200 `io.socket` connected successfully.
(for help, see: http://sailsjs.org/#!documentation/reference/BrowserSDK/BrowserSDK.html)
I tried using socket.io v1 and got the same weird situation. any ideas? thanks!
On server config/sockets.js
module.exports.sockets = {
onConnect: function(session, socket) {
sails.log.verbose('>>> socket user connected');
sails.sockets.blast('eventName', dataToBlast);
},
};
Documentation http://sailsjs.org/#/documentation/anatomy/myApp/config/sockets.js.html
On client
io.socket.on('eventName', function(dataToBlast) {
// process dataToBlast
});
Documentation http://sailsjs.org/#/documentation/reference/websockets/sails.io.js/io.socket.on.html
Be aware that socket needs to be subscribed to eventName. To subscribe you need to make controller SubscribeController.js and make request to it's action via socket.
var SubscribeController = {
sub: function(req, res) {
ModelName.subscribe(
req.socket,
[] /*records to subscribe to or empty array to subscribe to all */,
['eventName'] /* array of strings eventNames */
);
},
};
For debugging client you can connect to firehose which will give your client all messages from sails server
io.socket.get('/firehose');
io.socket.on('firehose', function newMessageFromSails(message) {
typeof console !== 'undefined' &&
console.log('New message published from Sails ::\n', message);
});