Switching from mqtts protocol to wss in nodeJS - javascript

I can connect to my MQTT broker, using mqtts protocol like this:
mqtt = require('mqtt')
let options = {
host: '0d9d5087c5b3492ea3d302aae8e5fd90.s2.eu.hivemq.cloud',
port: 8883,
path: '/mqtt',
clientId: 'mqttx_2d803d2743',
protocol: 'mqtts',
username: 'light29',
password: 'Mqtt29#!',
cleanSession: true,
}
// initialize the MQTT client
let client = mqtt.connect(options);
// setup the callbacks
client.on('connect', function () {
console.log('Connected');
});
client.on('error', function (error) {
console.log(error);
});
client.on('message', function (topic, message) {
// called each time a message is received
console.log('Received message:', topic, message.toString());
});
// subscribe to topic 'my/test/topic'
client.subscribe('mytest');
// publish message 'Hello' to topic 'my/test/topic'
client.publish('mytest', 'Hello');
This code, yields, as expected:
Connected
Received message: mytest Hello
Now, I would like to connect to this same broker but using wss protocol instead, thus I tried:
let options = {
host: '0d9d5087c5b3492ea3d302aae8e5fd90.s2.eu.hivemq.cloud',
port: 8884,
path: '/mqtt',
clientId: 'mqttx_2d803d2743',
protocol: 'wss',
username: 'light29',
password: 'Mqtt29#!',
cleanSession: true,
}
However, in this case, I got:
Error: connect ECONNREFUSED 127.0.0.1:8884
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 8884
}

I have finally managed to connect with these changes:
let options = {
username: 'light29',
password: 'Mqtt29#!',
}
// initialize the MQTT client
let client = mqtt.connect('wss://0d9d5087c5b3492ea3d302aae8e5fd90.s2.eu.hivemq.cloud:8884/mqtt', options);
Still, I do not know why explicitly passing protocol, host, port and path does solve it.

The problem in the original is that protocol in the options should be schema and should container URL schema e.g.
let options = {
host: '0d9d5087c5b3492ea3d302aae8e5fd90.s2.eu.hivemq.cloud',
port: 8883,
path: '/mqtt',
clientId: 'mqttx_2d803d2743',
schema: 'mqtts://',
username: 'light29',
password: 'Mqtt29#!',
cleanSession: true,
}
or
let options = {
host: '0d9d5087c5b3492ea3d302aae8e5fd90.s2.eu.hivemq.cloud',
port: 8883,
path: '/mqtt',
clientId: 'mqttx_2d803d2743',
schema: 'wss://',
username: 'light29',
password: 'Mqtt29#!',
cleanSession: true,
}
protocol is not a valid option.

Related

Heroku with Strapi, Application is not using production database

I've deployed my app to Heroku. It gives an Application Error message upon visit.
The logs gave me this:
[2021-02-15T01:04:05.882Z] debug ⛔️ Server wasn't able to start properly.
[2021-02-15T01:04:05.883Z] error Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
Which according to my guess, is that its trying to use local database. I think the app is not using the database.js located in config/env/production. The application runs fine with heroku local.
Below is the database.js I set for production env:
const parse = require("pg-connection-string").parse;
const config = parse(process.env.DATABASE_URL);
module.exports = ({ env }) => ({
defaultConnection: "default",
connections: {
default: {
connector: "bookshelf",
settings: {
client: "postgres",
host: config.host,
port: config.port,
database: config.database,
username: config.user,
password: config.password,
ssl: {
rejectUnauthorized: false,
},
},
options: {
ssl: true,
},
},
},
});
Creating and printing the config var on heroku console results in expected values.
For some reason the deployment method in the strapi docs to heroku does not seem to work when you initially have set up your local database as Postgres.
I had the same problem as you and I fixed it using the NODE_ENV env variable.
Instead of creating a new production database config file in ./config/production/database.js you can simply extend the config file in ./config/database.js with the prod config and decide based on what NODE_ENV is set which one to return.
As example:
module.exports = ({ env }) => {
const parse = require("pg-connection-string").parse;
const config = parse(env("DATABASE_URL", "127.0.0.1"));
const devConfig = {
client: "postgres",
connection: {
host: env("DATABASE_HOST", "127.0.0.1"),
port: env.int("DATABASE_PORT", 5432),
database: env("DATABASE_NAME", "db_name"),
user: env("DATABASE_USERNAME", "root"),
password: env("LOCAL_DB_PASSWORD"),
ssl: env.bool("DATABASE_SSL", false),
},
};
const prodConfig = {
client: "postgres",
connection: {
host: config.host,
port: config.port,
database: config.database,
user: config.user,
password: config.password,
ssl: {
rejectUnauthorized: false,
},
},
debug: false,
};
return
env("NODE_ENV", "development") === "production" ? prodConfig : devConfig
};

Why does couchDB gives me not authorized error when trying to create views

I am using couchDB and when ever i try to create views it is giving me this error
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 'EUNAUTHORIZED',
body: {
error: 'unauthorized',
reason: 'You are not a db or server admin.'
}
}
I am using Node-Couchdb and i am passing the credentials like this
const NodeCouchDb = require('node-couchdb')
require("dotenv-flow").config();
const couch = new NodeCouchDb({
host: process.env.DB_HOST,
protocol: process.env.DB_PROTOCOL,
port: process.env.DB_PORT
})
const couchAuth = new NodeCouchDb({
auth: {
user: process.env.DB_USER_NAME,
pass: process.env.PASSWORD
}
})
module.exports = {
couch
}
Your code is creating two instances of NodeCouchDB, couch and couchAuth where
couch points to server specified by envars without credentials
couchAuth points to the default server (127.0.0.1:5984) with credentials specified by envars
You need to combine parameters, for example
const NodeCouchDb = require("node-couchdb");
const couch = new NodeCouchDb({
host: process.env.DB_HOST,
protocol: process.env.DB_PROTOCOL,
port: process.env.DB_PORT,
auth: {
user: process.env.DB_USER_NAME,
pass: process.env.PASSWORD,
},
});
module.exports = {
couch
};

Error: connect ECONNREFUSED 127.0.0.1:465 send mail

I use the email account of my site contact#monsite.com to send smtp alert messages to the email of my users. but it still gives me this error
Error: connect ECONNREFUSED 127.0.0.1:465
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
errno: 'ECONNREFUSED',
code: 'ESOCKET',
syscall: 'connect',
address: '127.0.0.1',
port: 465,
command: 'CONN'
}
here is my code
// send mail function
exports.sendMailer = (optionsTransporterMail = OptionsTransporterMail, optionsMail = OptionsMail, callback) => {
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport(optionsTransporterMail);
transporter.sendMail(optionsMail, function (err, info) {
callback(err, info)
});
}
sendMailer({
service: 'mail.mywebsite.com',
port: 465,
secure: true,
auth: {
user: 'contact#mywebsite.com',
pass: '12345678'
}
}, {
from: '"My app <contact#mywebsite.com>',
to: user#domain.com,
subject: "Notify",
text: "Hello worlds",
html: "Hello worlds"
}, (err, info) => {
if err console.log(err);
console.log(info)
})
it still gives this error
Error: connect ECONNREFUSED 127.0.0.1:465
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
errno: 'ECONNREFUSED',
code: 'ESOCKET',
syscall: 'connect',
address: '127.0.0.1',
port: 465,
command: 'CONN'
}
I assume you want to use mail.mywebsite.com in order to send an email. In those terms, you have to set up host value in options, because it seems like by default it uses localhost (127.0.0.1).
Please try the following code:
sendMailer({
host: 'mail.mywebsite.com',
port: 465,
secure: true,
auth: {
user: 'contact#mywebsite.com',
pass: '12345678'
}
}, {
from: '"My app <contact#mywebsite.com>',
to: user#domain.com,
subject: "Notify",
text: "Hello worlds",
html: "Hello worlds"
}, (err, info) => {
if err console.log(err);
console.log(info)
})

cant conennect to db from my node.js app using mysql packege

must your help!!!
it throw me
Error: connect ECONNREFUSED 10.166.186.156:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14)
here my code :
const express = require("express");
const mysql = require("mysql");
const app = express();
const db = mysql.createConnection({
host: "DESKTOP-2QNN9FA",
user: "DuduLog",
password: "1234",
database: "testDB"
});
db.connect(err => {
if (err) {
console.log(err);
} else console.log("MySql Connected...");
});
app.listen("3000", () => {
console.log("Server started on port 3000");
});
what worng with it? thanks a lot
try use component mysql-ssh.
const connect = mysqlssh.connect({
host: 'xxxxxx',
user: 'xxxxxxx',
password: 'xxxxxx',
port: xx
},
{
host: '127.0.0.1',
user: 'xxxxx',
password: 'xxxxx',
database: 'xxxxxx',
port: 'xxxx'
})
if you use firebase functions you need to configure the panel with a payment method that supports connections to other servers

Nodemailer was sending mails but then it suddenly stopped, now it's giving me this error

This worked like some days ago but then it stopped. I really don't know what's going on. PS: I am on a Mac and it does not look like it's a firewall issue.
exports.sendEmail = (recipient, subject) => {
const mailTransporter = nodeMailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS
}
})
const mailOptions = {
from: process.env.EMAIL_USER,
to: recipient,
subject,
html: htmlTemplate(generatePasswordResetLink(),recipient),
}
return mailTransporter.sendMail(mailOptions, (error, success) => {
if (error) {
console.log(error);
return error;
}
return success;
})
The console.log(error); in the code spits this
{ Error: connect ECONNREFUSED 74.125.133.109:465
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
errno: 'ECONNREFUSED',
code: 'ESOCKET',
syscall: 'connect',
address: '74.125.133.109',
port: 465,
command: 'CONN' }
And I have tried like everything possible to get it to work. I have also restarted my laptop.
PS:
user: 'a valid gmail account',
pass: 'correct pass for the account'

Categories

Resources