Encountering "Error: connect ECONNREFUSED 127.0.0.1:6379..." when running Google Cloud Function connecting to a Redis Instance - redis

Im running a function to ingest data to a Redis instance. However, I am encountering an issue where I can't seem to connect to the client.
Here is the part of the code where it connects to the server.
const redis = require('redis');
require('dotenv').config;
const REDISHOST = process.env.REDISHOST;
const REDISPORT = 6379;
const REDISAUTH = process.env.AUTHSTRING;
const client = redis.createClient({
port: REDISPORT,
host: REDISHOST,
password: REDISAUTH
});
await client.connect();
Here is the whole error message:
"Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16)
at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)"
Any help would be appreciated. Thanks
I have tried using ioredis instead of redis but it shows a different error altogether.

It's trying to connect to localhost and Redis doesn't exist there. IF not given a hostname, this is the default behavior of Redis.
Looking at your code, this is probably because the REDISHOST environment variable isn't defined.

Related

mssql in nodejs not connecting

Trying to connect to my database to create a local website but having connection issues.
Running in node.js, this is my server.js file:
let sql = require('mssql');
let config = {
user: 'sa',
password: 'password',
server: '10.0.1.130\\SQLSERVER',
database: 'MY_DB',
};
function connect() {
let dbConn = new sql.ConnectionPool(config);
dbConn.connect()
}
connect();
Upon running this I get the following error:
ConnectionError: Failed to connect to 10.0.1.130:undefined - self signed certificate
code: 'ESOCKET'
Not sure why it is removing \SQLSERVER but that seems to be my issue.
I know the credentials are all correct as I connect to this on another computer (ubuntu) but its being removed and we want to move it to windows, have not been able to connect to this point.
Any suggestions are appreciated.

How can I send UDP data in Deno?

I've tried using the following code in my measure.ts script:
Deno.DatagramConn.send(...)
When I run my script like this: deno run --unstable --allow-all measure.ts I get the following error:
Property 'DatagramConn' does not exist on type 'typeof Deno'. 'Deno.DatagramConn' is an
unstable API. Did you forget to run with the '--unstable' flag?
This error seems to simultaneously deny and confirm the existence of the Deno.DatagramConn API
Similarly I've tried
Deno.connect({transport : 'udp'})
but this gives me the follow error (which probably makes sense as UDP is 'connectionless'):
Type '"udp"' is not assignable to type '"tcp"
I seem to have figured it out. I actually need to first listen on a socket, and then send data on it.
const addr : Deno.NetAddr = {transport: "udp", port: 8125, hostname: "1.2.3.4"};
const socket = await Deno.listenDatagram({
port: 0,
transport: "udp",
hostname: "0.0.0.0"
});
socket.send(new Uint8Array(), addr);
It's easy when you know how ¯\_(ツ)_/¯

Unable to connect Ganache with Truffle/Npm Dev server

I am able to work with Truffle and Ganache-cli. Have deployed the contract and can play with that using truffle console
truffle(development)>
Voting.deployed().then(function(contractInstance)
{contractInstance.voteForCandidate('Rama').then(function(v)
{console.log(v)})})
undefined
truffle(development)> { tx:
'0xe4f8d00f7732c09df9e832bba0be9f37c3e2f594d3fbb8aba93fcb7faa0f441d',
receipt:
{ transactionHash:
'0xe4f8d00f7732c09df9e832bba0be9f37c3e2f594d3fbb8aba93fcb7faa0f441d',
transactionIndex: 0,
blockHash:
'0x639482c03dba071973c162668903ab98fb6ba4dbd8878e15ec7539b83f0e888f',
blockNumber: 10,
gasUsed: 28387,
cumulativeGasUsed: 28387,
contractAddress: null,
logs: [],
status: '0x01',
logsBloom: ... }
Now when i started a server using "npm run dev". Server started fine but is not connecting with the Blockchain
i am getting the error
Uncaught (in promise) Error: Contract has not been deployed to detected network (network/artifact mismatch)
This is my truffle.js
// Allows us to use ES6 in our migrations and tests.
require('babel-register')
module.exports = {
networks: {
development: {
host: '127.0.0.1',
port: 8545,
network_id: '*', // Match any network id
gas: 1470000
}
}
}
Can you please guide me how i can connect ?
Solve the issue.
issue was at currentProvider, i gave the url of ganache blockchain provider and it worked.
if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source like Metamask")
// Use Mist/MetaMask's provider
// window.web3 = new Web3(web3.currentProvider);
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
} else {
console.warn("No web3 detected. Falling back to http://localhost:8545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
In your truffle.js, change 8545 to 7545.
Or, in Ganache (GUI), click the gear in the upper right corner and change the port number from 7545 to 8545, then restart. With ganache-cli use -p 8545 option on startup to set 8545 as the port to listen on.
Either way, the mismatch seems to be the issue; these numbers should match. This is a common issue.
Also feel free to check out ethereum.stackexchange.com. If you want your question moved there, you can flag it and leave a message for a moderator to do that.

Express server crashing due to MongoDB connection loss

I am having issues with an HTTP Node.js server built with:
Ubuntu 14.04
MongoDB 3.0.4
iojs v2.3.3
express=4.10.*
mongodb=1.4.34
The following middleware are being used:
app.use(response_time());
app.use(body_parser.urlencoded({extended: true}));
app.use(body_parser.json());
var MongoClient = require('mongodb').MongoClient;
app.use(function (req, res, next) {
var connection_options = {auto_reconnect: false};
MongoClient.connect(config.server.db, connection_options, function (err, db) {
if (err) {
log.error(err); // Logging error.
return next(err);
}
req.db = db;
next();
});
});
The server started running at 20:40:10 and successfully handled multiple requests.
At 02:59:02, the following error started to get logged on every request:
02:59:02.114Z ERROR CrowdStudy: failed to connect to [127.0.0.1:27017]
Error: failed to connect to [127.0.0.1:27017]
at null.<anonymous> (/home/ncphillips/Projects/crowdstudy/node_modules/mongodb/lib/mongodb/connection/server.js:555:74)
at emitThree (events.js:97:13)
at emit (events.js:175:7)
at null.<anonymous> (/home/ncphillips/Projects/crowdstudy/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:156:15)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/home/ncphillips/Projects/crowdstudy/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at emitErrorNT (net.js:1237:8)
My initial suspicion was that I was that the connection pool was filling up because I don't have anything to handle calling req.db.close(). I thought that passing in the options {auto_reconnect: false} would fix this issue by automatically closing the connection after some time, but it seems I was wrong.
Note that restarting the server fixes the issue, so I believe the problem has to do with Node rather than Mongo.
If this has to do with the connection pool, is there some setting I can pass to fix this, or can I have an end-ware that makes sure the connection always gets closed?
Thanks a lot to anyone who can help me out!
autoReconnect is an option that should be passed to the server configuration:
MongoClient.connect(config.server.db, {
server : { autoReconnect : false }
}, ...);
The documentation contains some errors: it states that the default setting is false (which it isn't), and it also states that autoReconnect should be set in an object called socketOptions (which it shouldn't).
You can add various event listeners to the db object that gets passed back, to detect when the connection to the database got closed/reconnected/...:
db.on('close', function(reason) { ... });
db.on('reconnect', function(db) { ... });
More events here.

socket.io redis ECONNREFUSED

my host has allowed ports 8000 to 8010
var express = require('express')
, https = require('https')
, app = express()
, cluster = require('cluster')
, numCPUs = require('os').cpus().length
, RedisStore = require("socket.io/lib/stores/redis")
, redis = require("socket.io/node_modules/redis");
, pub = redis.createClient(8002)
, sub = redis.createClient(8002)
, client = redis.createClient(8002);
, server = https.createServer(options,app);
, io = require('socket.io').listen(server);
io.set('store',new RedisStore({redisPub:pub,redisSub:sub,redisClient:client}));
if(cluster.isMaster){for(var i=0;i<numCPUs;i++){cluster.fork();}}
else{
console.log(numCPUs);
io.sockets.on('connection',function(socket){/*do stuff*/});
server.listen(8002);
}
I'm having real trouble understanding if I can't connect to redis because of the port or wether the redis supplied with socket.io is turned on...
events.js:72
throw er; // Unhandled 'error' event
^
Error: Redis connection to 127.0.0.1:8002 failed - connect ECONNREFUSED
at RedisClient.on_error (/home/engine/public_html/node_modules/socket.io/node_modules/redis/index.js:149:24)
at Socket.<anonymous> (/home/engine/public_html/node_modules/socket.io/node_modules/redis/index.js:83:14)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:426:14
at process._tickCallback (node.js:415:13)
[root#vps ~]# info - socket.io started
also, I don't understand how this can give me extra workers (I console.log(numCPUs) and it says 1, so does that mean I only have 1 worker? If so, this means that there is no change to performance; making this endeavour pointless!?).
You'll need to install and run your own instance of redis and then use the node client to connect.
See this thread to get more information to do what you are trying to do.
Examples in using RedisStore in socket.io