Node JS Events Emitter on net.createserver - node.js

So i got a problem and stuck around 3 days, i'm using node js as a socket server to receive string and processing some json data to give a string response to socket client, stack used in node js(net, events), my code have no problem for the first request, but after the second request it got an error like this(server):
[2020-12-23T07:45:13.821Z] server listening on port: 4444
[2020-12-23T07:45:15.696Z] New Connection : ::ffff:127.0.0.1
[2020-12-23T07:45:15.701Z] message: string
[2020-12-23T07:45:15.702Z] executing call function
[2020-12-23T07:45:15.703Z] oncall function
[2020-12-23T07:45:15.703Z] executing emit
[2020-12-23T07:45:15.704Z] {
orig: 'orig',
origParam: { number: '123456' },
resp: [
{
content: [Array],
firstPage: true,
lastPage: true,
number: 0,
numberOfElements: 1,
size: 1,
sort: null,
totalElements: 1
},
{ result: 'UNMATCHED' }
]
}
[2020-12-23T07:45:15.707Z] Returning : diterima
[2020-12-23T07:45:42.403Z] New Connection : ::ffff:127.0.0.1
[2020-12-23T07:45:42.407Z] message: string
[2020-12-23T07:45:42.408Z] executing call function
[2020-12-23T07:45:42.408Z] oncall function
[2020-12-23T07:45:42.409Z] executing emit
[2020-12-23T07:45:42.409Z] {
orig: 'orig',
origParam: { number: '123456' },
resp: [
{
content: [Array],
firstPage: true,
lastPage: true,
number: 0,
numberOfElements: 1,
size: 1,
sort: null,
totalElements: 1
},
{ result: 'UNMATCHED' }
]
}
[2020-12-23T07:45:42.410Z] Returning : diterima
[2020-12-23T07:45:42.411Z] {
orig: 'orig',
origParam: { number: '123456' },
resp: [
{
content: [Array],
firstPage: true,
lastPage: true,
number: 0,
numberOfElements: 1,
size: 1,
sort: null,
totalElements: 1
},
{ result: 'UNMATCHED' }
]
}
[2020-12-23T07:45:42.412Z] Returning : diterima
events.js:288
throw er; // Unhandled 'error' event
^
Error: This socket has been ended by the other party
at Socket.writeAfterFIN [as write] (net.js:451:14)
at HanaconsResponded.<anonymous> (C:\Users\ralfian\--\--\--.js:47:11)
at HanaconsResponded.emit (events.js:323:22)
at callFunction (C:\Users\ralfian\--\--\--.js:79:23)
at Socket.<anonymous> (C:\Users\ralfian\--\--\--.js:35:17)
at Socket.emit (events.js:311:20)
at addChunk (_stream_readable.js:294:12)
at readableAddChunk (_stream_readable.js:271:13)
at Socket.Readable.push (_stream_readable.js:209:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
Emitted 'error' event on Socket instance at:
at emitErrorNT (net.js:1336:8)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EPIPE'
}
and bellow is error on the client side at second request, (first request have no problem):
CLIENT: I connected to the server.
{"orig":"orig","origParam":{"number":"123456"},"resp":[{"content":[{"gender":"not match"}],"firstPage":true,"lastPage":true,"number":0,"numb
erOfElements":1,"size":1,"sort":null,"totalElements":1},{"result":"UNMATCHED"}]}
events.js:288
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read'
}
Here is my server Code:
var net = require('net');
var server = net.createServer();
require('log-timestamp');
var hostport = 4444;
var EventEmitter = require('events');
class CheckResponded extends EventEmitter {}
var checkResponded = new CheckResponded();
server.listen({
port : hostport,
exclusive: true
},);
console.log('server listening on ' + 'port: ' + hostport);
server.on('connection', (e) => {
console.log( 'New Connection : ' + e.remoteAddress );
e.setEncoding('utf8');
e.setTimeout(60000);
e.on('end', () => {})
e.on( 'timeout', () => {
console.log('Socket Timeout. Reseting.');
e.end();
});
e.on( 'data', (buff) => {
console.log("message: " + typeof buff);
try {
if (buff === "exec"){
console.log("executing call function")
callFunction()
}
} catch(error){
console.log('error on socket -> ' + error)
}
});
checkResponded.on( 'event1', (f) => {
console.log(f)
var rmsg;
rmsg = f;
console.log("Returning : " + 'diterima');
e.write(JSON.stringify(rmsg));
});
});
function callFunction() {
console.log('oncall function')
var imsg = {
"orig": "orig",
"origParam": {
"number": "123456",
},
"resp": [
{
"content": [
{
"gender": "not match",
}
],
"firstPage": true,
"lastPage": true,
"number": 0,
"numberOfElements": 1,
"size": 1,
"sort": null,
"totalElements": 1
},
{
"result": "UNMATCHED"
}
]
}
console.log('executing emit')
checkResponded.emit('event1', imsg);
return
}
and bellow is the client request example:
const net = require('net');
var host = 'localhost';
const client = net.createConnection({ port: 4444, host: host }, () => {
console.log('CLIENT: I connected to the server.');
client.write('exec')
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('CLIENT: I disconnected from the server.');
});
looking for help , Thanks!

One thing I see is that your client code assumes that your entire response arrives in one data event. That is not a safe assumption. It may sometimes happen that way, but it is not guaranteed and if the server is not done sending data when you call client.end(), you will get an error (like you are) on the server such as:
Error: This socket has been ended by the other party
You will need some sort of protocol in the data you send so that you can parse the incoming data and you can then know when you have a complete response and when you need to wait for the rest of the data to arrive.
Similar issue discussed here: Detect complete data received on 'data' Event in Net module of Node.js

Related

Node.js redis#4 Upgrade: SocketClosedUnexpectedlyError: Socket closed unexpectedly

I've got some legacy code that I'm upgrading from version 3 of the Node.js redis library to version 4 of the Node.js redis library. The basic shape of the code looks like this
var redis = require('redis')
var client = redis.createClient({
port: '6379',
host: process.env.REDIS_HOST,
legacyMode: true
})
client.connect()
client.flushall(function (err, reply) {
client.hkeys('hash key', function (err, replies) {
console.log("key set done")
client.quit()
})
})
console.log("main done")
When I run this code with redis#4.3.1, I get the following error, and node.js exits with a non-zero status code
main done
key set done
events.js:292
throw er; // Unhandled 'error' event
^
SocketClosedUnexpectedlyError: Socket closed unexpectedly
at Socket.<anonymous> (/Users/astorm/Documents/redis4/node_modules/#redis/client/dist/lib/client/socket.js:182:118)
at Object.onceWrapper (events.js:422:26)
at Socket.emit (events.js:315:20)
at TCP.<anonymous> (net.js:673:12)
Emitted 'error' event on Commander instance at:
at RedisSocket.<anonymous> (/Users/astorm/Documents/redis4/node_modules/#redis/client/dist/lib/client/index.js:350:14)
at RedisSocket.emit (events.js:315:20)
at RedisSocket._RedisSocket_onSocketError (/Users/astorm/Documents/redis4/node_modules/#redis/client/dist/lib/client/socket.js:205:10)
at Socket.<anonymous> (/Users/astorm/Documents/redis4/node_modules/#redis/client/dist/lib/client/socket.js:182:107)
at Object.onceWrapper (events.js:422:26)
at Socket.emit (events.js:315:20)
at TCP.<anonymous> (net.js:673:12)
While in redis#3.1.2 it runs (minus the client.connect()) without issue.
I've been able to work around this by replacing client.quit() with client.disconnect(), but the actual code is a little more complex than the above example and I'd rather use the graceful shutdown of client.quit than the harsher "SHUT IT DOWN NOW" of client.disconnect().
Does anyone know what the issue here might be? Why is redis#4 failing with a SocketClosedUnexpectedlyError: Socket closed unexpectedly error.
What I found so far is that after a while (keepAlive default is 5 minutes) without any requests the Redis client closes and throws an error event, but if you don't handle this event it will crash your application.
My solution for that was:
/* eslint-disable no-inline-comments */
import type { RedisClientType } from 'redis'
import { createClient } from 'redis'
import { config } from '#app/config'
import { logger } from '#app/utils/logger'
let redisClient: RedisClientType
let isReady: boolean
const cacheOptions = {
url: config.redis.tlsFlag ? config.redis.urlTls : config.redis.url,
}
if (config.redis.tlsFlag) {
Object.assign(cacheOptions, {
socket: {
// keepAlive: 300, // 5 minutes DEFAULT
tls: false,
},
})
}
async function getCache(): Promise<RedisClientType> {
if (!isReady) {
redisClient = createClient({
...cacheOptions,
})
redisClient.on('error', err => logger.error(`Redis Error: ${err}`))
redisClient.on('connect', () => logger.info('Redis connected'))
redisClient.on('reconnecting', () => logger.info('Redis reconnecting'))
redisClient.on('ready', () => {
isReady = true
logger.info('Redis ready!')
})
await redisClient.connect()
}
return redisClient
}
getCache().then(connection => {
redisClient = connection
}).catch(err => {
// eslint-disable-next-line #typescript-eslint/no-unsafe-assignment
logger.error({ err }, 'Failed to connect to Redis')
})
export {
getCache,
}
anyway... in your situation try to handle the error event
client.on('error', err => logger.error(`Redis Error: ${err}`))

Node.JS aws-sdk getting "socket hang up" error

I am trying to get the pricing information of AmazonEC2 machines using "#aws-sdk/client-pricing-node" package.
Each time I will send a request to get the pricing information, process it, and send the request again, until all the information is obtained (no NextToken anymore). The following is my code.
const {
PricingClient,
} = require('#aws-sdk/client-pricing-node/PricingClient');
const {
GetProductsCommand,
} = require('#aws-sdk/client-pricing-node/commands/GetProductsCommand');
const agent = new https.Agent({
maxSockets: 30,
keepAlive: true,
});
const pricing = new PricingClient({
region: "us-east-1",
httpOptions: {
timeout: 45000,
connectTimeout: 45000,
agent,
},
maxRetries: 10,
retryDelayOptions: {
base: 500,
},
});
const getProductsCommand = new GetProductsCommand( { ServiceCode: 'AmazonEC2', });
async function sendRequest() {
let result = false;
while (!result) {
try {
const data = await pricing.send(getProductsCommand);
result = await handleReqResults(data);
} catch (error) {
console.error(error);
}
}
}
async function handleReqResults(data) {
// some data handling code here
// ...
//return false when there is "NextToken" in the response data
if (data.NextToken) {
setNextToken(data.NextToken);
return false;
}
return true;
}
The code will run for a while (variable time) and then stop with the following error:
{ Error: socket hang up
at createHangUpError (_http_client.js:332:15)
at TLSSocket.socketOnEnd (_http_client.js:435:23)
at TLSSocket.emit (events.js:203:15)
at TLSSocket.EventEmitter.emit (domain.js:448:20)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
code: 'ECONNRESET',
'$metadata': { retries: 0, totalRetryDelay: 0 } }
I had tried to run it on a GCP VM instance, and there was no such a problem. But the problem happens when I run it on my local machine.
Do anyone have any idea how to solve this problem?
(BTW: my node version is v10.20.1)

Elastic search gives Bad request for ping

Code in elasticsearch.js file
function es() {
throw new Error('Looks like you are expecting the previous "elasticsearch" module. ' +
'It is now the "es" module. To create a client with this module use ' +
'`new es.Client(params)`.');
}
es.Client = require('./lib/client');
es.ConnectionPool = require('./lib/connection_pool');
es.Transport = require('./lib/transport');
es.errors = require('./lib/errors');
module.exports = es;
var elasticsearch = require('elasticsearch')
var client = new es.Client({
host: 'localhost:9200',
log: 'trace',
})
// Ping the cluster
client.ping({
requestTimeOut: 30000,
},
function(error){
if(error) {
console.log(error)
console.error("elasticsearch cluster is down!")
}
else {
console.log("All is well")
}
})
and I am running elastic search locally with command $bin/elasticsearch
but when I do $node elasticsearch.js it gives the error saying
Elasticsearch INFO: 2018-01-22T11:17:50Z
Adding connection to http://localhost:9200/
Elasticsearch DEBUG: 2018-01-22T11:17:50Z
starting request {
"method": "HEAD",
"requestTimeout": 3000,
"castExists": true,
"path": "/",
"query": {
"requestTimeOut": 30000
}
}
Elasticsearch TRACE: 2018-01-22T11:17:50Z
-> HEAD http://localhost:9200/?requestTimeOut=30000
<- 400
Elasticsearch DEBUG: 2018-01-22T11:17:50Z
Request complete
{ Error: Bad Request
at respond (/Users/ElasticSearchServer/node_modules/elasticsearch/src/lib/transport.js:307:15)
at checkRespForFailure (/Users/ElasticSearchServer/node_modules/elasticsearch/src/lib/transport.js:266:7)
at HttpConnector.<anonymous> (/Users/ElasticSearchServer/node_modules/elasticsearch/src/lib/connectors/http.js:159:7)
at IncomingMessage.bound (/Users/ElasticSearchServer/node_modules/elasticsearch/node_modules/lodash/dist/lodash.js:729:21)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
status: 400,
displayName: 'BadRequest',
message: 'Bad Request',
path: '/',
query: { requestTimeOut: 30000 },
body: undefined,
statusCode: 400,
response: '',
toString: [Function],
toJSON: [Function] }
elasticsearch cluster is down!
If I try adding new index, delete index, check the health or search, it works fine and gives the appropriate result.
Can anyone help me to fix the issue? thanks in advance!
In the new JavaScript client every option that is not intended for Elasticsearch lives in a second object, your code should be updated as follows:
'use strict'
const { Client } = require('#elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })
client.ping({}, { requestTimeout: 20000 }, (err, response) => {
...
})
In the response object other than body, statusCode, and headers, you will also find a warnings array and a meta object, which should help you debug issues.
In this case, warnings contained the following message: 'Client - Unknown parameter: "requestTimeout", sending it as query parameter'.

How should I do to make an elasticSearch , search query from Node.js correctly?

I am stuck in a problem and can not figure out the solution.
I want to use an elasticSearch query from my nodejs.
The problem is, I can make it work from postman, but not from node.
http://user:psd#my_domain:9200/ra_autocomplete/search
And from my nodejs app :
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'my_domain',
port : 9200,
protocol : 'http',
auth : 'user:psd',
maxRetries : 2
});
And then,
client.search({
index: "ra_autocomplete",
body: {
query: {
m_prefix : {
r_n : {
query : my_var
}
}
}
}
} , function(err, res) {
console.log(err);
console.log(res);
});
I get this error :
Error: Not Found
at respond (my_path\node_modules\elasticsearch\src\lib\transport.js:307:15)
at checkRespForFailure
(my_path\node_modules\elasticsearch\src\lib\transport.js:266:7)
at HttpConnector.
(my_path\node_modules\elasticsearch\src\lib\connectors\http.js:159:7)
at IncomingMessage.bound
(my_path\node_modules\lodash\dist\lodash.js:729:21)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
status: 404,
displayName: 'NotFound',
message: 'Not Found',
path: '/roads_autocomplete/_search',
query: {},
body: '{"query":{"m_prefix":{"r_n":{"query":"montexte a analyser"}}}}',
statusCode: 404,
response: '\r\n404 Not Found\r\n\r\n404 Not Found\r\nnginx/1.10.3 (Ubuntu)\r\n\r\n\r\n',
toString: [Function],
toJSON: [Function] }
Any help would be appreciated, the problem is, when I try to make it with postman, it goes well.
Thank you.
I think the problem is that you are not connecting to elastic search node.
Add your port and domain to into one line and then start by running a simple query.
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
client.search({
index: 'products',
type: 'product',
body: {
query: {
bool: {
}
}
}
}).then((body) => {
return body;
}, (error) => {
console.trace(error.message);
});

MongoDB ECONNRESET error

I'm trying to setup a basic mongodb connection, but I get an ECONNRESET error. I've researched a bit and found similar questions and answers but haven't found a solution.
Code:
'use strict';
const Hapi = require('hapi');
const hapiMongo = require('hapi-mongodb');
var server = new Hapi.Server();
server.connection({
port: '1111'
});
server.route({
method: 'GET',
path: '/',
config: {
cors: true,
handler: (request, reply) => {
var db = request.server.plugins['hapi-mongodb'].db;
reply('Hello World' + db);
}
}
});
server.register({
register: hapiMongo,
options: {
"url": "mongodb://username:psswd#ds011308.mongolab.com:11308/heroku_jg542kf4",
"settings": {
"db": {
"native_parser": false
}
}
}
}, (err) => {
if (err) {
console.error(err);
throw err;
}
server.start((err) => console.log('Served at:', server.info.uri));
})
Console error:
{ [MongoError: server ds011308-a.mongolab.com:11308 received an error
{"name":"M ongoError","message":"read ECONNRESET"}] name:
'MongoError', message: 'server ds011308-a.mongolab.com:11308
received an error {"name":"Mong oError","message":"read ECONNRESET"}'
}
C:\Users\username\Desktop\code\hapi-mongo\node_modules\mongodb\lib\mongo_client.js:4
54
throw err
^ MongoError: server ds011308-a.mongolab.com:11308 received an error {"name":"Mon goError","message":"read ECONNRESET"}
at null. (C:\Users\username\Desktop\code\hapi-mongo\node_modules\mong
odb-core\lib\topologies\server.js:297:40)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at null. (C:\Users\username\Desktop\code\hapi-mongo\node_modules\mong
odb-core\lib\connection\pool.js:132:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket. (C:\Users\username\Desktop\code\hapi-mongo\node_modules\mo
ngodb-core\lib\connection\connection.js:132:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
Any pointers on how I can fix this?

Resources