socket.io-redis trailing bytes - node.js

I am using socket.io-emitter to broadcast an event to a set of channels with a for loop:
In the file, I have:
var io = require('socket.io-emitter')({
host: 'localhost',
port: 6379
});
module.exports = {
exampleFunction: function(req, res, next) {
var channels = req.param('channels'),
data = req.param('data');
for (var i=0; i<channels.length; i++) {
io.to(channels[i]).emit('example event', data)
}
}
}
In app.js, I have socket.io-redis:
io.adapter(socketio_redis({
host: 'localhost',
port: 6379,
pubClient: redis.createClient(6379, '127.0.0.1'),
subClient: redis.createClient(6379, '127.0.0.1')
}))
When I try to run exampleFunction, I get the following uncaught error in my console:
Error: 348 trailing bytes
at Object.decode (C:\Users\Website\socket.io-redis\node_modules\msgpack-js\msgpack.js:200:47)
at Redis.onmessage (C:\Users\Website\socket.io-redis\index.js:93:24)
at RedisClient.EventEmitter.emit (events.js:106:17)
at RedisClient.return_reply (C:\Users\Website\node_modules\redis\index.js:672:22)
at ReplyParser.<anonymous> (C:\Users\Website\node_modules\redis\index.js:309:14)
at ReplyParser.EventEmitter.emit (events.js:95:17)
at ReplyParser.send_reply (C:\Users\Website\node_modules\redis\lib\parser\javascript.js:300:10)
at ReplyParser.execute (C:\Users\Website\node_modules\redis\lib\parser\javascript.js:211:22)
at RedisClient.on_data (C:\Users\Website\node_modules\redis\index.js:534:27)
at Socket.<anonymous> (C:\Website\node_modules\redis\index.js:91:14)
I have seen that this is from msgpack. Have any of you encountered this error before? How did you resolve it?
Thank you.

On the main page of nm.socket.io-redis it is written:
Msgpack with giving us an error called "trailing bytes". After reading
up we realized that we could just use JSON.stringfy/JSON.parse instead
of msgpack.
Which looks like the error you are getting. As it is suggested there, try JSON.strigfy

Okay,
We found the solution. Refer to:
https://github.com/Automattic/socket.io-redis/issues/17
As you can see, socket.io-emitter requires you set return_buffers to true on your redis client.
Let me know if this works. Otherwise I can dif in the code some more.

This should answer your question.
https://github.com/Automattic/socket.io-redis/issues/17

This helped me with Error: -560815898 trailing bytes error:
npm install msgpack-js-v5
change file node_modules/socket.io-redis/index.js at the very top:
var msgpack = require('msgpack-js');
to
var msgpack = require('msgpack-js-v5');
now use code similar to this:
var adapter = require('socket.io-redis');
var pub = redis.createClient(6379, 'localhost');
var sub = redis.createClient(6379, 'localhost', { return_buffers: true });
io.adapter(adapter({pubClient: pub, subClient: sub}));
UPDATE:
In some days I stumbled upon an error bops.readUInt64BE is not a function, so eventually I switched msgpack-js-v5 to msgpack5 and now it works ok.

Related

SerialPort NodeJS - SerialPort is not a constructor

Good afternoon, I have a problem with Serial Port ,
I did everything as described in the documentation, but still I get an error SerialPort is not a constructor
const { SerialPort } = require('serialport')
const port = new SerialPort({
path: 'COM4',
baudRate: 9600,
autoOpen: false,
})
Spent several hours solving the problem, tried a lot of things and got to the official documentation, but there was an error here too :)
my nerves left the chat…

RESTIFY: Error: socket hang up] code: 'ECONNRESET' on multiple requests

I am implementing a node app, which brings in order details from BigCommerce.
Multiple calls are made to BigCommerce API asynchronously using Restify JsonClient.
It works fine for some calls but after that i gives error: [Error: socket hang up] code: 'ECONNRESET', sslError: undefined, body: {}
I have tried turning off socket pooling ie by setting agent=false, but it still gives same error.
Following is code which makes call to BigCommerce API
makeRequest = function (url, params, headers, orderDetails, cb) {
var options = {
headers: headers
};
var client = restify.createJsonClient({
url: url
});
client.get(options, function(err, req, res, obj) {
if(err){
console.log(err);
cb(err,obj);
} else if(obj != null) {
var result = obj;
if(orderDetails == null) {
cb(null,result);
} else {
cb(null, result , orderDetails);
}
}
});
};
I get following error:
{ [Error: socket hang up] code: 'ECONNRESET', sslError: unde
fined, body: {} } Error: socket hang up
at SecurePair.error (tls.js:993:23)
at EncryptedStream.CryptoStream._done (tls.js:689:22)
at CleartextStream.read [as _read] (tls.js:490:24)
at CleartextStream.Readable.read (_stream_readable.js:320:10)
at EncryptedStream.onCryptoStreamFinish (tls.js:301:47)
at EncryptedStream.g (events.js:175:14)
at EncryptedStream.EventEmitter.emit (events.js:117:20)
at finishMaybe (_stream_writable.js:352:12)
at endWritable (_stream_writable.js:359:3)
at EncryptedStream.Writable.end (_stream_writable.js:337:5)
at EncryptedStream.CryptoStream.end (tls.js:628:31)
at Socket.onend (_stream_readable.js:483:10)
Why am i getting such error? How can i handle it?
Thanks
I just wanted to make sure that you're setting the agent setting in the right area.
Include the
"agent": false
in your options. (It's not set in the options in the code you pasted)
Per gfpacheco in the comments here: https://github.com/restify/node-restify/issues/485
By default NodeJS uses agents to keep the TCP connection open, so you can reuse it.
The problem is that if the server is closed, or it closes your connection for whatever reason you get the ECONNRESET error.
To close the connection every time you just need to set agent: false in your client creation
I've tried this solution and it worked for me.
Other than that, the
"secureOptions": "constants.SSL_OP_NO_TLSv1_2"
solution posted here sounds like it could be the right path, since you're getting an sslError.
Maybe you are running into this issue https://github.com/joyent/node/issues/5360
TL;DR: You could try with latest node version and secureOptions: constants.SSL_OP_NO_TLSv1_2 added to your options.

NodeJS - How to handle "listen EADDRINUSE" when accessing external process

I'm using phantomJS for printing PDF, with phantomjs-node module. It works well but when I try to create several files at once, it throws an Unhandled error "Listen EADDRINUSE.
I assume this is because the module uses phantomJS which is an external process and it can't bind it to the same port several times ?
Anyway, I can't catch this error, and I'd like to resolve this problem at least by avoiding a server crash when this happens.
I thought of using a "global" variable, like a locker, in order to block concurrent calls until the current one is finished.
Any idea of how to implement that, or any other solution ?
The code from #AndyD is not correct imho. See lines 45 - 54 in
https://github.com/sgentle/phantomjs-node/blob/master/phantom.coffee
So the example should be
var portscanner = require('portscanner');
var phantom = require('phantom');
portscanner.findAPortNotInUse(40000, 60000, 'localhost', function(err, freeport) {
phantom.create({'port': freeport}, function(ph){
...
}
});
You should be able to pass in a port number every time you call create:
var phantom = require('phantom');
phantom.create(null, null, function(ph){
}, null, 11111);
You can then use a counter to ensure it's different every time you start phantomjs-node.
If you are starting a new process every time and you can't share a counter then you can use portscanner to find a free port:
var portscanner = require('portscanner');
var phantom = require('phantom');
portscanner.findAPortNotInUse(40000, 60000, 'localhost', function(err, freeport) {
phantom.create(null, null, function(ph){
...
}
}, null, freeport);

Mongoose Trying to open unclosed connection

This is a simplified version of the problem, but basically I'm trying to open 2 mongodb connections with mongoose and it's giving me "Trying to open unclosed connection." error.
Code sample:
var db1 = require('mongoose');
db1.connect('my.db.ip.address', 'my-db');
var db2 = require('mongoose');
db2.connect('my.db.ip.address', 'my-db');
db2.connection.close();
db1.connection.close();
Any idea how to make it work?
connect() opens the default connection to the db. Since you want two different connections, use createConnection().
API link: http://mongoosejs.com/docs/api.html#index_Mongoose-createConnection
To add on Raghuveer answer :
I would also mention that instead of using mongoose directly (you are probably using it this way you end up on this post) :
require('mongoose').model(...);
You would use the returned connection :
var db = require('mongoose').connect('xxx', 'yyy');
db.model(...);
I get this issue while running my tests.
This is what I did to solve it.
//- in my app.js file.
try {
mongoose.connect('mongodb://localhost/userApi2'); //- starting a db connection
}catch(err) {
mongoose.createConnection('mongodb://localhost/userApi2'); //- starting another db connection
}
I had this problem doing unit test with mocha.
The problem came when I added a second test because beforeEach is called twice.
I've solved this with this code:
const mongoose = require('mongoose');
describe('Your test suite', () => {
beforeEach( () => {
if (mongoose.connection.db) {
return; // or done();
} else {
// connect to mongodb
});
describe('GET /some-path', () => {
it('It should...', () => {
});
});
describe('POST /some-path', () => {
it('It should...', () => {
});
});
});
Hope it helps you!
You are attempting to open the default connection ( which is not yet closed ) a 2nd time.
do the following instead
var db = require('mongoose'); //note only one 'require' needed.
var connectionToDb1 = db.createConnection('my.db1.ip.address', 'my-db1');
var connectionToDb2 = db.createConnection('my.db2.ip.address', 'my-db2');
Using mongoose.disconnect(fn):
mongoose.disconnect(() => {
// here it would be possible "reset" models to fix
// OverwriteModelError errors
mongoose.models = {};
// here comes your logic like registering Hapi plugins
server.register(somePlugin, callback);
});
I found this question typing the error message and despite my problem is a bit different I believe it could be useful for those using Hapi. More specifically Hapi + rest-hapi + mocha.
When running mocha with --watch option I was facing both: OverwriteModelError and Error: Trying to open unclosed connection errors.
Simple Solution -
Use mongoose.createConnection() instead of mongoose.connect()
Its occurs because of version issue

Making an HTTP request using node.js throws EAFNOSUPPORT

I'm trying to make a simple HTTP GET request using node.js, but I'm running into trouble using node.js v0.3.4-pre (i.e. compiled from HEAD as of this morning). Here's my code:
var cli = require('cli');
var http = require('http');
var url = require('url');
cli.parse();
cli.main(function(args, opts) {
this.debug(args[0]);
var siteUrl = url.parse(args[0]);
var site = http.createClient(siteUrl.port, siteUrl.host);
console.log(siteUrl);
var request = site.request("GET", siteUrl.pathname, {'host' : siteUrl.host})
request.end();
request.on('response', function(response) {
response.setEncoding('utf8');
console.log('STATUS: ' + response.statusCode);
response.on('data', function(chunk) {
console.log("DATA: " + chunk);
});
});
});
Here's the error that I get:
node.js:68
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: EAFNOSUPPORT, Address family not supported by protocol family
at doConnect (net.js:499:19)
at Client.connect (net.js:652:30)
at Client._ensureConnection (http.js:1033:10)
at Client.request (http.js:1048:8)
at Object.<anonymous> (/Users/paul/Desktop/readify.js:16:21)
at /usr/local/lib/node/.npm/cli/0.2.3-2/package/cli.js:995:18
at Object.main (/usr/local/lib/node/.npm/cli/0.2.3-2/package/cli.js:1000:9)
at Object.<anonymous> (/Users/paul/Desktop/readify.js:10:5)
at Module._compile (node.js:359:32)
at Object..js (node.js:367:14)
Found the bug, siteUrl.port will be undefined unless the URL explicitly names a port. So, the solution is:
var site = http.createClient(siteUrl.port || 80, siteUrl.host);
var site = http.createClient(siteUrl.port, siteUrl.host);
should rather be
var site = http.createClient(siteUrl.port || 80, siteUrl.hostname);
The same error message appeared on my very old XPSP2-box for ANY connect()-attempts. E.g npm wasn't able to do anything, and simple http requests failed.
While trying to find a solution, this post appeared all over the place, but its not the same issue.
In my case it had to do with WSAIoctl(...) always returning WSAEOPNOTSUPP when querying for WSAID_CONNECTEX, which seemed strange. This led me to a post recommending doing a "netsh winsock reset" from the cmd, which fixed the problem!

Resources