GRPC client Error: 14 UNAVAILABLE: failed to connect to all addresses - javascript

Im trying to test my grpc client connection using below code.
I have .net core grpc server and using node js grpc client to connect. But getting "failed to connect to all addresses" error. BUt able to connect .net grpc server to .net grpc client. Any help much appreciated.
Not sure if im missing anything from below grpc client code.
'use strict';
//Same as the other projects we import fs for reading documents, in this case employees.js json
const fs = require('fs');
//Importing GRPC and the proto loader
const grpc = require('grpc');
const loader = require('#grpc/proto-loader');
//reads the proto
const packageDefinition = loader.loadSync('Repository.proto', {
keepCase: false,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
//Loads the proto file to be used in constant pkg
const pkg = grpc.loadPackageDefinition(packageDefinition);
//Creates server
const PORT = 5001;
//console.log(pkg);
const client = new pkg.repository.Repository('localhost:5001', grpc.credentials.createInsecure());
client.GetUpdates({}, function (err, response) {
console.log("----Response error----");
if (err) {
console.log(err);
} else {
console.log(response);
}
});
im getting below error:
{ Error: 14 UNAVAILABLE: failed to connect to all addresses
at Object.exports.createStatusError (/mnt/c/Users/ht9638/Desktop/workspace/current-workspace/gRPCNodeJS/firstService/node_modules/grpc/src/common.js:91:15)
at Object.onReceiveStatus (/mnt/c/Users/ht9638/Desktop/workspace/current-workspace/gRPCNodeJS/firstService/node_modules/grpc/src/client_interceptors.js:1209:28)
at InterceptingListener._callNext (/mnt/c/Users/ht9638/Desktop/workspace/current-workspace/gRPCNodeJS/firstService/node_modules/grpc/src/client_interceptors.js:568:42)
at InterceptingListener.onReceiveStatus (/mnt/c/Users/ht9638/Desktop/workspace/current-workspace/gRPCNodeJS/firstService/node_modules/grpc/src/client_interceptors.js:618:8)
at callback (/mnt/c/Users/ht9638/Desktop/workspace/current-workspace/gRPCNodeJS/firstService/node_modules/grpc/src/client_interceptors.js:847:24)
code: 14,
metadata: Metadata { _internal_repr: {}, flags: 0 },
details: 'failed to connect to all addresses' }
Can someone please help me on this issue.

I have the same issue when I tried to follow gRPC nodejs tutorials from the official documentation
Root cause(for me): port 50051 are the default port used from the main documentation, when i check the list of the port being used on my laptop, turn out that port 50051 used by "NVIDIA Web Helper.exe".
How i find this out: I tried to change the port slightly to 50052 and console.log the err and it shows as expected

Related

How to connect nodejs redis client to redis cloud server?

const redis = require('redis');
const client = redis.createClient({
host: 'redis-19606.redislabs.com',
port: 19606,
password: 'password'
});
client.on('ready', () => {
console.log('redis is connected');
});
client.on('error', (err) => {
console.log('redis is disconnected: ', err);
});
(async () => {
try {
await client.connect();
} catch (error) {
console.error('error while connecting redis', error);
}
})();
This somehow does not seem to work. What am I doing wrong? It keeps connecting to 127.0.0.1:6379 which is the default config instead of what I am passing. This is only happening with nodejs client of redis. go-redis the golang client for redis is working flawlessly.
Just a guess, but which Node Redis version do you use? I had difficulties upgrading myself. The configuration of the client has changed since version 4.x.x. Since version 4.x.x you have to use a confiuration according to Client Configuration. Therefore use
const client = redis.createClient({
socket: {
host: 'redis-19606.redislabs.com',
port: 19606,
}
});
or use a URL
const client = redis.createClient({
url: "redis://redis-19606.redislabs.com:19606"
});
Your client configuration matches the Node Redis version 3.x.x but not 4.x.x. Please see Redis NPM v3.1.2 and Redis NPM v4.0.1 for details.
you should maintain this format of Redis connecting string:
redis://:YOUR_PASSWORD#YOUR_ENDPOINT:YOUR_PORT
What version of redis are you using?
Do not explicitly conect to redis by writing "client.connect()" but
instead use this after setting host, port and password:
redisClient.on("connect", () => {})
and then you can use redisClient variable to get and set values.
Also if you're trying to connect to a redislabs server then I think your host name might be incorrect and double check your host from redislabs.
Try this new Redis("redis://username:authpassword#127.0.0.1:6380/4");
Check the documentation Link

Example node.js ftp server?

I need to create a node.js app that connects to this ftp server and downloads files from this directory:
ftp://www.ngs.noaa.gov/cors/rinex/2021/143/nynb
I've tried following the ftp npm package docs but I feel like I am doing something horribly wrong:
import Client from "ftp";
/**
* https://github.com/mscdex/node-ftp
*/
const c = new Client();
c.on("ready", function () {
c.get(
"ftp://www.ngs.noaa.gov/cors/rinex/2021/143/nynb",
(error, stream) => {
if (error) throw error;
console.log(`stream`, stream);
stream.once("close", function () {
c.end();
});
}
);
});
// connect to localhost:21 as anonymous
c.connect();
When I run npm run dev with nodemon I get:
Error: connect ECONNREFUSED 127.0.0.1:21
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
[nodemon] app crashed - waiting for file changes before starting...
Can someone please help? I'm completely stumped.
Is it possible if someone could show me a small example of how I can connect to this remote ftp server?
There are a few points :
You're connecting to the local ftp with c.connect();. You need to connect to www.ngs.noaa.gov to download files from there.
This path cors/rinex/2021/143/nynb is a directory on the remote host. c.get doesn't work, you need to list all files in the directory then download them 1 by 1.
The code below connect to the remote server and list all files in the directory
const Client = require('ftp');
const fs = require("fs");
const c = new Client();
c.on('ready', function () {
c.list( "/cors/rinex/2021/143/nynb", function (err, list) {
if (err) throw err;
console.dir(list);
});
});
c.connect({
host: "www.ngs.noaa.gov",
});

Using ssl with node mysql and Cleardb

I have a free ClearDB account that I got through Heroku (as a resource)
It gives me the
CA certificate
Client cert
Client Key
These are all in .pem format
const fs = require('fs');
const ssl = {
ca: fs.readFileSync('cleardb-ca.pem', "utf-8"),
key: fs.readFileSync('cleardb-key.pem', "utf-8"),
cert: fs.readFileSync('cleardb-cert.pem', "utf-8")
};
const cleardb = mysql.createConnection({
host:"HOST",
user: "USER",
password: "PWD",
database: "DB",
ssl: ssl
});
cleardb.connect((err) => {
if (err) throw err;
console.log('connected!');
});
This keeps throwing
Error: 22928:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol:c:\ws\deps\openssl\openssl\ssl\statem\statem_lib.c:1929:
and then the error stacktrace shows an Object like
library: 'SSL routines', function: 'ssl_choose_client_version', reason: 'unsupported protocol', code: 'HANDSHAKE_SSL_ERROR', fatal: true
The SSL routines:ssl_choose_client_version:unsupported error is the same exact error I get when I try connecting to this DB through the MySQL CLI.
I.e. mysql --host=HOST --ssl-capath=. --ssl-cert=cleardb-cert.pem --ssl-key=cleardb-key.pem --user=USER
I'm able to connect through both Node and the CLI if I do not use SSL.
Any idea what's going on here?
Should I really worry that much about using SSL? I've read that if the option for transport security exists, that you should elect to use it.

unable to connect to database from node script and there is no error thrown

I have been working with nodejs google cloud functions for a while. I have been facing a weird issue where I can't connect to database servers and there is no error logged not even timeout. I am using node v14.2.0 with pg as postgres library. My nodejs code is
const { Client } = require('pg');
let connectionSetting = {
host: "host",
user: "user",
database: "db_name",
password: "password",
};
const client = new Client(connectionSetting);
console.log(connectionSetting);
client.connect(err => {
if (err) {
console.error(`Error connecting to db -> ${err}`);
}
console.log("Connection established with PgSql DB ");
});
There are no console logs or whatever.
This same code is working on other systems. The database is remote database hosted on gcp and I'm able to connect to it using tablePlus as GUI client.
Any help appreciated.
I found the issue. It has to do with the node version. I was using node current 14.2.0 so I installed node lts 12 and then everything works fine.
Thanks for all help

MongoDB/Mongoose Connection Error when Dbname is used in the connection string

A NodeJS app uses mongoose 5.6.0 to connect to MongoDB 4.0.10 which runs on localhost inside a docker container.
The connection can be established when we use
const mongoUri = 'mongodb://admin:mypassword#127.0.0.1:27017'
mongoose.connect(mongoUri)
Problem: We start getting authentication errors when we include the name of the database we are connecting to. There is no problem using Python to connect to the same MongoDB database.
const mongoUri = 'mongodb://admin:mypassword#127.0.0.1:27017/my-db'
mongoose.connect(mongoUri)
and also tried
const mongoUri = 'mongodb://admin:mypassword#127.0.0.1:27017/my-db'
mongoose.connect(mongoUri, { useNewUrlParser: true })
UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: Authentication failed.
Why is it unable to make the connection and how can we solve this problem?
Update
Found the solution to be
const mongoUri = 'mongodb://admin:mypassword#127.0.0.1:27017'
mongoose.connect(mongoUri, { useNewUrlParser: true, dbName: 'my-db' })
Why must the dbname be passed as an option instead of including it in the connection string?
This has worked for me, thanks.
var result = await mongoose.connect('mongodb://root:example#localhost:27017/', {useNewUrlParser: true,dbName: 'my-db'})
Short answer: Add ?authSource=admin to URI string or use {authSource:"admin"}
const mongoUri = 'mongodb://admin:mypassword#127.0.0.1:27017/my-db?authSource=admin'
mongoose.connect(mongoUri)
Follow this answer https://stackoverflow.com/a/68137961/12280326 for detailed explanation.

Categories