Node.js spawn vlc child process not working in express - node.js

I'm using node.js and express to create a controller of sorts for my home media centre that can be operated through a browser.
I'm having a lot of trouble spawning the VLC process, but strangely only it works when executed from the node prompt, just not when run from file.
Here is what I have tested:
In the node command line/interpreter: require('child_process').spawn('vlc');
This works as I would expect, I can see the vlc window opens and persists on screen.
If I take the exact same line of code and I place it in another file (say test.js) and run that from the command line with node test.js nothing happens. Logging out the child process object a few seconds later gives me this:
{ _closesNeeded: 3,
_closesGot: 3,
signalCode: null,
exitCode: 1,
killed: false,
_internal: null,
pid: 11837,
stdin:
{ _handle: null,
_pendingWriteReqs: 0,
_flags: 0,
_connectQueueSize: 0,
destroyed: true,
bytesRead: 0,
bytesWritten: 0,
allowHalfOpen: undefined,
writable: false,
readable: false,
_connecting: false,
_connectQueue: null,
_idleNext: null,
_idlePrev: null },
stdout:
{ _handle: null,
_pendingWriteReqs: 0,
_flags: 1,
_connectQueueSize: 0,
destroyed: true,
bytesRead: 0,
bytesWritten: 0,
allowHalfOpen: undefined,
writable: false,
readable: false,
_events: { close: [Function] },
_connecting: false,
_connectQueue: null,
_idleNext: null,
_idlePrev: null },
stderr:
{ _handle: null,
_pendingWriteReqs: 0,
_flags: 1,
_connectQueueSize: 0,
destroyed: true,
bytesRead: 215,
bytesWritten: 0,
allowHalfOpen: undefined,
writable: false,
readable: false,
_events: { close: [Function] },
_connecting: false,
_connectQueue: null,
_idleNext: null,
_idlePrev: null } }
What is particularly weird about this is that I can launch other programs in the same way and it works. For instance replacing 'vlc' with 'gedit' results in the text editor appearing exactly as expected.
Anyone got ideas as to what this could be?

This is just a hunch, but I bet you are prematurely exiting your test before the VLC process even gets "running" so to speak.
Execute it like this:
var spawn = require('child_process').spawn;
var vlc = spawn('vlc');
vlc.on('exit', function(code){
console.log('Exit code: ' + code);
//EXIT TEST HERE
});
Spawn Documentation.
Edit:
Just saw 'test.js' and thought you were running a test. Post the relevant part of your Express code.

Related

AWS elasticache - Redis: Unable to debug for Redis errors

We are implementing caching using AWS Elasticache with Redis and are using nodejs for connecting it with the Redis. Creating a sorted set and adding and getting elements to and from it. It is working fine on local machine, while not working on Staging. Here is the code,
const redis = require("redis")
const redisOption = {
host: redis_host_url,
port: redis_port
}
let client = redis.createClient(redisOption);
console.log(client);
client = promisify(client.zadd).bind(client);
console.log(client);
let response = await client("abc",1,"string_to_add");
console.log(response);
Getting response printed on local machine, but Lambda logs does not contain the last console. Client is:
RedisClient {
_events: [Object: null prototype] { newListener: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
address: 'XXXXXXXXXX:6379',
connection_options: { port: 6379, host: 'XXXXXXXXXX', family: 4 },
connection_id: 0,
connected: false,
ready: false,
should_buffer: false,
command_queue:
Denque {
_head: 0,
_tail: 0,
_capacityMask: 3,
_list: [ <4 empty items> ] },
offline_queue:
Denque {
_head: 0,
_tail: 0,
_capacityMask: 3,
_list: [ <4 empty items> ] },
pipeline_queue:
Denque {
_head: 0,
_tail: 0,
_capacityMask: 3,
_list: [ <4 empty items> ] },
connect_timeout: 3600000,
enable_offline_queue: true,
retry_timer: null,
retry_totaltime: 0,
retry_delay: 200,
retry_backoff: 1.7,
attempts: 1,
pub_sub_mode: 0,
subscription_set: {},
monitoring: false,
message_buffers: false,
closing: false,
server_info: {},
auth_pass: undefined,
selected_db: undefined,
fire_strings: true,
pipeline: false,
sub_commands_left: 0,
times_connected: 0,
buffers: false,
options:
{ host: XXXXXXXXXX,
port: 6379,
socket_keepalive: true,
socket_initial_delay: 0,
return_buffers: false,
detect_buffers: false },
reply: 'ON',
reply_parser:
JavascriptRedisParser {
optionReturnBuffers: false,
optionStringNumbers: false,
returnError: [Function: returnError],
returnFatalError: [Function: returnFatalError],
returnReply: [Function: returnReply],
offset: 0,
buffer: null,
bigStrSize: 0,
totalChunkSize: 0,
bufferCache: [],
arrayCache: [],
arrayPos: [] },
stream:
Socket {
connecting: true,
_hadError: false,
_handle:
TCP {
reading: false,
onread: [Function: onStreamRead],
onconnection: null,
[Symbol(owner)]: [Circular] },
_parent: null,
_host: null,
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: true,
paused: false,
emitClose: false,
autoDestroy: false,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
_events:
[Object: null prototype] {
end: [Array],
connect: [Function],
data: [Function],
error: [Function],
close: [Function],
drain: [Function] },
_eventsCount: 6,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: false,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
[Symbol(asyncId)]: 7,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0 } }
Even console after the promisify is working, and is printing:
[Function: bound zadd]
But the last console is not printing anything. I am not able to debug as, AWS Elasticache doesn't offer logs for Redis.
You need to define a bastion host in your AWS setup and locally in ~/.ssh/config
Host your-bastion-host
HostName <IP_of_your_bastion_host_on_EC2>
Port 22
User ec2-user
IdentityFile ~/.ssh/<your_key_file_for_bastion_host>.pem
ForwardAgent yes
AddKeysToAgent yes
Using a Bastion host and a VPC, you are able to connect to the AWS Elasticache service by SSH port forwarding...
ssh your-bastion-host -Cv -N -L 6390:ec-replication-group-xyz.amazonaws.com:6379
..and "redis-cli" tool (It makes sense to forward the Redis port 6379 to a different port locally if you already have a Redis server installed locally)
redis-cli -h localhost -p 6390
info
monitor
Then you can use in "redis-cli" the monitor command to display every command processed by the Redis server and the info command to get the most important information and statistics about the Redis server.

Cannot connect to Eclipse Mosquitto broker using Node.js mqtt library

I am running Eclipse Mosquitto on a local docker container on 172.17.0.2:1883
I am able to publish/subscribe using MQTT.fx client but now trying to connect to the broker from node and haven't been able to.
The topic is "sensors"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e747c86b6ec1 eclipse-mosquitto "/docker-entrypoint.…" 5 hours ago Up 5 hours 0.0.0.0:1883->1883/tcp, 0.0.0.0:9001->9001/tcp adoring_varahamihira
When I inspect the IP address
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' e747c86b6ec1
172.17.0.2
I am trying to use the following node server to connect to my MQTT broker but the 'connect' event never seems to happen.
// Note: I have an express server running but didn't include
const mqtt = require("mqtt")
var MQTT_TOPIC = "sensors";
var MQTT_ADDR = "mqtt://172.17.0.2";
var MQTT_PORT = 1883;
var client = mqtt.connect(MQTT_ADDR, {
port: MQTT_PORT,
clientId: 'bgtestnodejs232323',
protocolId: 'MQIsdp',
protocolVersion: 3,
connectTimeout: 1000,
debug: true
});
client.on('connect', function () {
console.log("connected!")
});
client.on('error', function (err) {
console.log(err)``
client.end()
})
When I run:
console.log(util.inspect(client))
The output is:
MqttClient {
options:
{ protocol: 'mqtt',
slashes: true,
auth: null,
host: '172.17.0.2',
port: 1883,
hostname: '172.17.0.2',
hash: null,
search: null,
query: [Object: null prototype] {},
pathname: null,
path: null,
href: 'mqtt://172.17.0.2',
clientId: 'bgtestnodejs232323',
protocolId: 'MQIsdp',
protocolVersion: 3,
connectTimeout: 1000,
debug: true,
defaultProtocol: 'mqtt',
keepalive: 60,
reschedulePings: true,
reconnectPeriod: 1000,
clean: true,
resubscribe: true,
customHandleAcks: [Function] },
streamBuilder: [Function: wrapper],
outgoingStore: Store { options: { clean: true }, _inflights: Map {} },
incomingStore: Store { options: { clean: true }, _inflights: Map {} },
queueQoSZero: true,
_resubscribeTopics: {},
messageIdToTopic: {},
pingTimer: null,
connected: false,
disconnecting: false,
queue: [],
connackTimer:
Timeout {
_called: false,
_idleTimeout: 1000,
_idlePrev: [TimersList],
_idleNext: [TimersList],
_idleStart: 478,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(unrefed)]: false,
[Symbol(asyncId)]: 9,
[Symbol(triggerId)]: 1 },
reconnectTimer: null,
_storeProcessing: false,
_packetIdsDuringStoreProcessing: {},
nextId: 26898,
outgoing: {},
_firstConnection: true,
_events:
[Object: null prototype] {
close: [ [Function], [Function], [Function] ],
connect: [Function] },
_eventsCount: 2,
_maxListeners: undefined,
stream:
Socket {
connecting: true,
_hadError: false,
_handle:
TCP {
reading: false,
onread: [Function: onStreamRead],
onconnection: null,
[Symbol(owner)]: [Circular] },
_parent: null,
_host: null,
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: [Writable],
pipesCount: 1,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: true,
paused: false,
emitClose: false,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
_events:
[Object: null prototype] {
end: [Array],
data: [Function: ondata],
error: [Function: nop],
close: [Function] },
_eventsCount: 4,
_maxListeners: 1000,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 34,
writing: false,
corked: 1,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: [Object],
lastBufferedRequest: [Object],
pendingcb: 9,
prefinished: false,
errorEmitted: false,
emitClose: false,
bufferedRequestCount: 9,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
[Symbol(asyncId)]: 5,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0 } }
I have been trying the suggestions in this answer: Why is MQTT not connecting with NodeJS?
Thanks for your help!
The IP address range 172.17.0.0/16 that is handed out to Docker containers by the Docker engine are part of the RFC1918 set intended for private use.
In the case of Docker, they are used on the internal "bridge" network and only accessible from other docker containers or the machine hosting the Docker engine.
The 0.0.0.0:1883->1883/tcp, 0.0.0.0:9001->9001/tcp output for the info of your mosquitto container shows that these ports have been exposed and are mapped to the host machines IP address. You should be able to connect if you change 172.17.0.2 for the IP address of the machine hosting the Docker engine.
Port 9001 is the websocket listener
Port 1883 is the native MQTT listener
You are using the mqtt:// schema for your broker URL which indicates the native MQTT protocol, yet you are forcing the use of port 9001.
If you want to use WebSockets then you should change the schema to ws://
If you want to use native MQTT then you need to change the port to 1883

Discord.js bot keeps getting random errors

so I'm running my bot by typing "node ." in the console and it runs like normal, but after some random time it gives me this error:
It's run on node.js and the bot works fine normally until the error randomly shows. Like I said, it just randomly happens, if tried to find a correlation with commands run in my server and the error showing up, but nothing seems to show. I start my bot as follows:
const Discord = require("discord.js");
const client = new Discord.Client();
client.login("myTokenGoesHere");
the error:
ErrorEvent {
target:
WebSocket {
_events:
[Object: null prototype] {
message: [Function],
open: [Function],
error: [Function],
close: [Function] },
_eventsCount: 4,
_maxListeners: undefined,
readyState: 2,
protocol: '',
_binaryType: 'nodebuffer',
_finalize: [Function: bound finalize],
_closeFrameReceived: false,
_closeFrameSent: false,
_closeMessage: '',
_closeTimer: null,
_finalized: true,
_closeCode: 1006,
_extensions: {},
_isServer: false,
_receiver:
Receiver {
_binaryType: 'nodebuffer',
_extensions: null,
_maxPayload: 0,
_bufferedBytes: 0,
_buffers: null,
_compressed: false,
_payloadLength: 393,
_fragmented: 0,
_masked: false,
_fin: true,
_mask: null,
_opcode: 1,
_totalPayloadLength: 0,
_messageLength: 0,
_fragments: null,
_cleanupCallback: null,
_isCleaningUp: false,
_hadError: false,
_loop: false,
add: [Function: bound add],
onmessage: null,
onclose: null,
onerror: null,
onping: null,
onpong: null,
_state: 0 },
_sender:
Sender {
_extensions: {},
_socket: [TLSSocket],
_firstFragment: true,
_compress: false,
_bufferedBytes: 0,
_deflating: false,
_queue: [] },
_socket:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: 'gateway.discord.gg',
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 4,
connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: 'gateway.discord.gg',
_readableState: [ReadableState],
readable: false,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: null,
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: null,
timeout: 0,
[Symbol(res)]: [TLSWrap],
[Symbol(asyncId)]: 29,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 19058858,
[Symbol(kBytesWritten)]: 19205,
[Symbol(connect-options)]: [Object] },
_error: null,
url: 'wss://gateway.discord.gg/?v=6&encoding=json',
_req: null },
type: 'error',
message: 'read ECONNRESET',
error:
{ Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:111:27) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' } }
If you loon at the error it says:
message: 'read ECONNRESET',
This means that the Connection reset this could because of:
Your Internet having issues/lag
Issues at Cloudflares/Discords Servers
Your OS put itself into Sleepmode and cut the connection
If your bot crashes because of it add:
client.on('error', error => {
console.log(error);
// Do something eg. Log the Error
});

Nodejs readline giving some error

I want to read a file line by line and based on the data in each line i have to categorise the data.
var rd = readline.createInterface({
input: fs.createReadStream('/home/user/Desktop/text.txt'),
output: process.stdout,
console: false
});
When this line is executed the file is read and printed in the terminal.
But when I try to read the file line by line using readline I am getting error.
rd.on('line', (input) => {
console.log(input);
});
I am getting the following error.
`Interface {
_sawReturnAt: 0,
isCompletionEnabled: true,
_sawKeyPress: false,
_previousKey:
{ sequence: '\n',
name: 'enter',
ctrl: false,
meta: false,
shift: false },
domain:
Domain {
domain: null,
_events: { error: [Function: debugDomainError] },
_eventsCount: 1,
_maxListeners: undefined,
members: [] },
_events: { line: [ [Function], [Function], [Function] ] },
_eventsCount: 1,
_maxListeners: undefined,
output:
WriteStream {
connecting: false,
_hadError: false,
_handle:
TTY {
bytesRead: 0,
_externalStream: {},
fd: 9,
writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread] },
_parent: null,
_host: null,
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
domain: null,
_events:
{ end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
resize: [Object] },
_eventsCount: 4,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 1,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 22151,
_sockname: null,
_writev: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
columns: 80,
rows: 24,
_type: 'tty',
fd: 1,
_isStdio: true,
destroySoon: [Function],
destroy: [Function] },
input:
ReadStream {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 65536,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: true,
endEmitted: true,
reading: false,
sync: false,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
domain:
Domain {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
members: [] },
_events: { end: [Object], data: [Function: onData] },
_eventsCount: 2,
_maxListeners: undefined,
path: '/home/user/Desktop/text.txt',
fd: null,
flags: 'r',
mode: 438,
start: undefined,
end: undefined,
autoClose: true,
pos: undefined,
bytesRead: 31,
destroyed: true,
closed: true },
historySize: 30,
crlfDelay: 100,
_prompt: '> ',
terminal: true,
line: '',
cursor: 0,
history: [ '1,2,3', '4,8,4,8', '2,2,2,2', '1,2,3,4' ],
historyIndex: -1,
prevRows: 0,
paused: true,
closed: true }`
Please help I am new to this.
Your code looks fine but you are using the Node.js REPL rather than running the code from a file, which is why you're seeing this behaviour.
The Node.js REPL (what you get when you type node in terminal) will store variables. However, when only an identifier is used the value is also returned. What you're seeing isn't an error, but the rd object.
The Node.js REPL is good for testing but, unless you have a specific reason for using it, the best thing to do is create an app.js file, add your code to it and then run node app.js.
If you do need to write multi-line code using REPL, then be sure to type .editor once initialised, so that whitespace, etc. is interpreted correctly.
Also, from the 7.7.2 docs console isn't an option which createInterface is expecting, so that can be removed. In your particular example, you could also remove output: process.stdout, as your logging each line using the line event

Very large object dumped on each request

I have an application that I'm writing using express.js. When I run the application on my laptop, on each request, I see a very large object dumped as JSON to the console. It starts like this and goes on for many lines:
{ domain: null,
_events: null,
_maxListeners: 10,
socket:
{ domain: null,
_events:
{ drain: [Function: ondrain],
timeout: [Object],
error: [Function],
close: [Object] },
_maxListeners: 10,
_handle:
{ writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread] },
_pendingWriteReqs: 0,
_flags: 0,
_connectQueueSize: 0,
destroyed: false,
errorEmitted: false,
bytesRead: 483,
_bytesDispatched: 0,
allowHalfOpen: true,
writable: true,
readable: true,
server:
The odd thing is that the exact same code doesn't do this on my PC. I guess this isn't an error as such but it means I can't see any useful output in the terminal. I'm at a loss as to why I'm getting this output.
I resolved the problem by deleting the node_modules directory in my project and than running npm install to re-download the required modules. Clearly something wasn't right in one of the modules.

Resources