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.
Related
recently I've been trying to create a WebSocket server (using the ws library for node.js). At first I used the ws unencrypted protocol, but then I had to switch to wss. This brought some client authentication issues. When the client (running on a browser)
(client.js)
this.socket = new WebSocket(`wss://ipv4.address:port`);
... tries to connect to the Node.js-based server
(server.mjs:)
const server = createServer({
cert: readFileSync('/path/to/ssl_certificate.cer'),
key: readFileSync('/path/to/private_key.key'),
ca: [
readFileSync('/etc/ssl/certs/ca-certificates.crt'),
readFileSync('/path/to/ssl_certificate_INTERMEDIATE.cer')
],
rejectUnauthorized: false
});
const wss = new WebSocketServer({ server });
server.listen(port, "hostname", () => {
//the server actually listens, so this line of code is printed
console.log(`listening on wss://${wss.address().address}:${server.address().port}`);
});
server.on("tlsClientError", (err, tlsSocket) => {
console.error("TLS client error", err);
tlsSocket.destroy();
});
... it goes into "tlsClientError", printing this:
TLS client error [Error: C0D71E8ECB7F0000:error:0A000416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown:../deps/openssl/openssl/ssl/record/rec_layer_s3.c:1584:SSL alert number 46
] {
library: 'SSL routines',
reason: 'sslv3 alert certificate unknown',
code: 'ERR_SSL_SSLV3_ALERT_CERTIFICATE_UNKNOWN'
}
This looks like the server isn't willing to accept the client's certificates. How do I set them? Is there something I'm not understanding about WebSockets over TLS?
I tried following the advice of many answers from StackOverflow, disabling rejectUnauthorized, but the node https server is still failing, even after adding the same SSL certificates my website is using (same hostname, different port)
EDIT:
I forgot to mention, that connecting from the same host as the server works (i.e. using the ws client part on the node side), as per https://github.com/websockets/ws/blob/master/examples/ssl.js, and even when disabling rejectUnauthorized (because I'm not using a self-signed certificate)
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');
});
I created my Socket server with Express, SocketIO and Redis
server.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
server.listen(8890, function (e) {
console.log(e);
});
io.on('connection', function (socket) {
console.log("new client connected");
var redisClient = redis.createClient();
redisClient.subscribe('message');
redisClient.on("message", function(channel, message) {
console.log("mew message in queue "+ message + "channel");
socket.emit(channel, message);
});
socket.on('disconnect', function() {
redisClient.quit();
});
socket.on('connect_error', function() {
redisClient.quit();
});
});
From command line, I run node server.js. Its worked.
I also created a html file to connect to that server.
From Browser Console, I run io.connect('http://localhost:8890'). I got as the result
As I see, too much connections (requests).
What happens? What wrong from my code?
You have mismatched client and server versions causing the initial connection to fail and the older client is dumb enough to just keep trying over and over again. If you are using 2.0.4 on the server, then you must use that version for the client too. If you serve the client version directly from your server with:
<script src="http://localhost:8890/socket.io/socket.io.js"></script>
Then, the socket.io server will automatically give you the right client version.
Or, you can manually link to the right version on your CDN such as https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js. But client and server versions MUST match.
The advantage of getting the client directly from your own server is that anytime you update your server version of socket.io, the client version will automatically be upgraded for you and kept in perfect sync since the matching client version is built into the server version.
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}
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.