Connecting to Named Unix Domain Socket Node - javascript

I am trying to connect to a named Unix Domain Socket via nodejs. I have seen that the docs seem to support connecting to Unix Sockets, however I haven't seen any examples of connecting to a socket by name, and not by accessing a socket file at a well known location.
I can clearly see that the socket I need to connect to is being created by using lsof (and some grepping):
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
run 5632 user 3u unix 0xffff8803dd4b6000 0t0 29647 ##user#5632#1 type=SEQPACKET
The name of the socket is being passed to the script that actually runs my node script. I have tried the following:
import net = require('net');
var socket;
var element = "#user#5632#1"; //Parsed from args
try {
socket = net.createConnection("#"+element,(error)=>{
if(error){
console.log(error)
}else{
console.log("Connection Established "+element);}
});
socket.on('error', function(err) {
console.log("Error: " + err);
});
} catch (error) {
console.error(error);
}
Clearly, I have this wrapped up to catch errors in a few places(at different points in execution), but the point is that createConnection is throwing:
Error: Error: connect ENOENT ##user#5882#1
Using net.connectcauses the exact same error.
I tested creating a socket (file) in a well known location, and connecting to is, and that worked just fine, but as far as I have determined, node, or at least the net module does not seem to support connecting to ephemeral sockets.
Anybody know if there is a way to do this, or if I need to format my socket name differently in order to connect or any help really?

Node does not support SEQPACKET sockets. You may submit a PR to the node issue tracker as a feature request to add support and/or you may need to write a node addon that lets you connect to SEQPACKET sockets.

Related

IOT tcp connection error with nodejs and net

I have created a server using net module:
// Creating and connecting with server
const net = require('net');
const server = net.createServer(); //Creating server
//Connecting with server
server.on('connection', function (socket) {
let remoteAddress = `${socket.remoteAddress},${socket.remotePort}`
console.log(remoteAddress)
console.log(`connection is established... ${Date.now()} \n `);
socket.write(`connection is established...${Date.now()} \n`);
//Receiving and Sending payload from/to client
socket.on('data', async function (payload) {
console.log("payload from client",payload)
socket.write(`acknowledge : ${payload}`);
});
//Close connection
socket.on('close', function () {
console.log('Server Connection Closed');
});
//Server error
socket.on('error', function (err) {
console.log("Caught flash policy server socket error: ")
console.log(err.stack)
});
});
server.listen(8001, function () {
console.log('Server Listing on Port 8001');
})
I deploy this code to AWS EC2 and When I tried to connect with telnet client (telnet ec2_ip 8001), initailly it is working but after sometime it is giving following errors.
screenshot of telnet client:
screenshot of ec2 logs:
And When I tried to connect with real IOT scooter with ec2 Ip address and port 8001, It is not connecting for even a second.
Can anyone please tell me what I am doing wrong?
Note: I don't have much knowledge about IoT. This is the first time I am connecting an IoT scooter with nodejs.
It is hard to know exactly what your problem is without seeing more of your code, or getting more explained. Like what happens in your real code? The logs seem to indicate that you are making outbound calls to another service after the initial connection. If that is the case, are you handling error conditions of your dependencies correctly? An unhandled exception would kill your server, and it would need to be restarted to work again. If you are using dependencies that fail, for instance databases, other servers, configuration files etc that fail on your scooter, that may be why it fails without even allowing one single connection. But without knowing more specifics about your application and architecture, it is just wild guesses at this point.
Your server example code, seems fine. As long as you have opened for traffic on that port in your routing configuration in AWS, it should respond. It could be that you just simply run out of connections, but again that requires me to know more to give you a definitive answer.
My advice would be to check your dependencies, and your logs for supporting systems.

Trigger a javascript function from nodejs backend

I'm building an interactive aplication for kids. I'm working with some sensors that sends UDP data and I need to do something with that on screen.
I want to work with node js in order to receive these udp commands (that's fine for me). The problem is that I can't find the way that node trigger some function to the frontend javascript vanilla. Is there a way to do that? I know I can comunicate java with node using post and emit, but for that, java must make the request first and wait for a concrete answer, in this case, frontend must be always waiting for node commands.
Thanks in advance!
It is possible to push from the server to the client if you first establish a connection using Websockets. The alternative approach is to frequently poll the server.
To use websockets, grab a library such as ws by using npm install ws and basically follow their docs. Example from their docs:
import { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});

Connect to XMPP server using xmpp.js in the browser

I'm trying to run a test using a local xmpp server in the browser.
import { client } from '#xmpp/client';
const xmpp = client({
service: 'xmpp://localhost:5222/',
username: 'user',
password: 'pass',
});
xmpp.start().catch(err => {
console.error('start failed', err);
});
But I get the following error:
No compatible connection method found.
From what I've read, the browser expects a websocket connection instead of an xmpp connection.
The xmpp.js documentation says that it supports websockets as well, but I'm not sure what I have to change in order to successfully connect. Do I have to add WS support to my XMPP server?
Most likely you would have to change your service URI - both protocol as well as port part. As per xmpp.js client documentation you should use:
service: 'ws://localhost:5280/xmpp-websocket',
Exact path may be xmpp-server dependent (i.e. xmpp-websocket may not be needed) - please check your server's documentation.

How to connect to elasticache in my case?

I am trying to connect to aws elasticache from my app.
I know the endpoint and the port but for some reason I can't connect to it.
I used this npm package:
https://www.npmjs.com/package/node-memcached-client
code:
const Memcached = require('node-memcached-client');
const client = new Memcached({
host: 'mycache.aa11c.0001.use2.cache.amazonaws.com', //fake aws cache endpoint
port: 11211
});
console.log(client); // I can see it outputs stuff
client.connect()
.then(c => {
console.log('connected');
console.log(c);
}).catch(function(err){
console.log('error connecting');
console.log(err);
});
For some reason when I run the codes, all I see is
[Memcached] INFO: Nothing any connection to mycache.aa11c.0001.use2.cache.amazonaws.com:11211, created sid:1
no errors or connected message in the console.log. Am I doing something wrong here?
Thanks!
You may want to go over the below document from AWS to access Elasticache resources from outside of AWS:
Access AWS Elasticache from outside
I would recommend setting up a local memcached instance for development and debugging, and connect to Elasticache from an EC2 instance in test and production environments.
The ROI for trying to setup NAT and mapping the IP addresses is not justifiable for dev/test unless absolutely necessary.
I ended using port forwarding to bypass the local instance can't connect to elasticache issue.
by using
ssh -L 11211:{your elasticache endpoint}:11211 {your ec2 instance ip}

Using Paho to connect to Mosquito MQTT server

I am trying to use Paho's JavaScript library to connect to a Mosquito MQTT server. I have just used the example on the Paho website. Which is:
function onConnect() {
console.log("onConnect");
client.subscribe("harleyRowland/myTopic");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "harleyRowland/myTopic";
client.send(message);
}
onConnect();
On Safari, I get the following error:
Error: AMQJS0011E Invalid state not connected.
and on Chrome, I get the following error as well as the one above:
WebSocket connection to 'ws://mosquitoServer.co.uk:1883/mqtt' failed:
Error during WebSocket handshake: net::ERR_CONNECTION_RESET
I need to use the Mosquito server for this work. Is it possible using JavaScript? If it isn't possible, is there another web technology I could use?
You need to add a websockets listener to your mosquitto setup. you do this by adding something like the following to your mosquitto.conf
listener 1884
protocol websockets
This will listen for MQTT over websockets on port 1884 (not 1883 as you can not run both on the same socket with mosquitto).
You will also need to be running mosquitto newer than 1.4 and not on Windows unless you compile it yourself

Categories