Connection failed - trying to connect to the mosquitto broker using React native - javascript

I tried to bind MQTT with the react native. When trying to connect to the broker, when I run my code, After a few minutes I get the following error:
here is the error Object { "errorCode": 7, "errorMessage":
"AMQJS0007E Socket error:undefined.", "invocationContext":
undefined, }
import init from 'react_native_mqtt'
import AsyncStorage from '#react-native-async-storage/async-storage'
init({
size: 10000,
storageBackend: AsyncStorage,
defaultExpires: 1000 * 3600 * 24,
enableCache: true,
reconnect: true,
sync : {
}
});
constructor(){
super();
this.onConnect = this.onConnect.bind(this)
const client = new Paho.MQTT.Client('52.11.11.11', 1883, "clientId-" + parseInt(Math.random() * 100, 10));
client.connect({
onSuccess: this.onConnect,
userName: "user",
password: "pass",
onFailure: (e) => {console.log("here is the error" , e); }
});
this.state = {
message: [''],
client,
messageToSend:'',
isConnected: false,
};
}
onConnect = () => {
const { client } = this.state;
console.log("Connected!!!!");
this.setState({isConnected: true, error: ''})
};

We solved this by edit the mosquitto config file to either add a new listener port 8883 and to use the websocket protocol for that port
https://stackoverflow.com/a/32309525/12166187

Related

KafkaJSConnectionError: Connection timeout. SSL configuration on Kafka.js

The credentials that I am provided with :- (I also have kafka.keystore.jks and kafka.truststore.jks)
host: xxxxx-xxxxx-x.cloudclusters.net
port: xxxxx
ip: xxx.xxx.xxx.xx
trustore pw: xxxxxxxx
keystore pw: xxxxxxxx
import WebSocket from 'ws';
import express from 'express'
import { Kafka } from 'kafkajs';
import { Partitioners } from 'kafkajs';
import jks from 'jks-js';
import fs from 'fs';
const keystore = jks.toPem(
fs.readFileSync('./kafka.keystore.jks'),
'mypassword'
);
const trustore = jks.toPem(
fs.readFileSync('./kafka.truststore.jks'),
'mypassword'
);
const {
caroot: {ca},
localhost: {key,cert} } = keystore;
// const { caroot: {ca} } = trustore;
console.log("**************** kafka.keystore.jks ****************");
// console.log(keystore)
console.log("ca ===>", ca); // getting the keys
console.log("key ===>", key);
console.log("cert ===>", cert);
console.log("**************** kafka.truststore.jks ****************");
// console.log(trustore)
// console.log("ca ===>", ca);
// setting up kafka
const kafka = new Kafka({
clientId: 'qa-topic',
brokers: ['xxxx.cloudclusters.net:xxxx'], // HOST:PORT
ssl: {
rejectUnauthorized: false,
ca: ca,
key: key,
cert: cert
},
})
const producer = kafka.producer({ createPartitioner: Partitioners.DefaultPartitioner })
producer.on('producer.connect', () => {
console.log(`KafkaProvider: connected`);
});
producer.on('producer.disconnect', () => {
console.log(`KafkaProvider: could not connect`);
});
producer.on('producer.network.request_timeout', (payload) => {
console.log(`KafkaProvider: request timeout ${payload.clientId}`);
});
await producer.connect().catch((e) => {
console.log("ERROR happened ==>",e) // Getting Connection Timeout Error
})
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`I am listening at ${port}`);
});
I am currently using SSL Configuration, following the Kafka.js Documentation. I have gotten ca,key,cert as strings from my kafka.keystore.jks. I am passing them in the SSL Object but when I try to connect my producer, I get the following error :-
cause: KafkaJSConnectionError: Connection timeout
at Timeout.onTimeout [as _onTimeout]

Knex.js connection error when rerunning the function

I'm using Knex to connect to an Azure database, run a query that returns the status of a database (COPYING/ONLINE).
If I run this once, all is fine.
But if I use a setInterval to rerun this (I want to know when the status changes from COPYING to ONLINE) I'm getting a connection error the second, and third, and.. time the function is called.
Here is my code
const knex = require('knex')({
client: 'mssql',
connection: {
host: '***',
user: '***',
password: '***',
options: { requestTimeout: 350000, encrypt: true },
},
pool: {
min: 0,
max: 15,
},
});
async function copyStatus() {
try {
console.log('Running query');
const status = await knex.raw(
"SELECT name, state_desc FROM sys.databases WHERE name = 'Tide_QA_Dev_runtime' "
);
return status[0].state_desc;
// console.log(status[0].state_desc);
} catch (error) {
console.log(error);
} finally {
console.log('Closing connection with database');
await knex.destroy();
}
}
function intervalFunc() {
copyStatus().then(function (result) {
if (result === 'ONLINE') {
console.log('Database copy is done.');
} else if (result === 'Database is still copying') {
console.log('bezig');
}
});
}
setInterval(intervalFunc, 2000);
Here is my output
Closing connection with database
Database copy is done.
Running query
Error: Unable to acquire a connection
at Client_MSSQL.acquireConnection (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/client.js:286:13)
at Runner.ensureConnection (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/execution/runner.js:259:46)
at Runner.run (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/execution/runner.js:30:30)
at Raw.Target.then (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/builder-interface-augmenter.js:24:43)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
Closing connection with database
Running query
Error: Unable to acquire a connection
at Client_MSSQL.acquireConnection (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/client.js:286:13)
at Runner.ensureConnection (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/execution/runner.js:259:46)
at Runner.run (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/execution/runner.js:30:30)
at Raw.Target.then (/Users/davidbouckaert/Documents/Qite/TIDE_repo/node_modules/knex/lib/builder-interface-augmenter.js:24:43)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
Closing connection with database```
It looks like the connection is made (see console log: Running query).
Any idea what's going on?
You should use code like below, it works for me.
const knex = require('knex')({
client: 'mssql',
connection: {
// no tcp:
server: 'j***2sqlserver.database.windows.net',
user: 'j***2',
password: 'J****0',
database: 'yourdbname',
port: 1433,
options: { requestTimeout: 350000, encrypt: true },
},
pool: {
min: 0,
max: 15,
},
});
My Test Result.
You should not call knex.destroy() if you are going to make more queries to the DB.
Move that knex.destroy() call to somewhere just before application is going to exit.

Cant get connection with Socket.IO from Angular to NodeJS

I am trying to solve this for hours.. if someone can help me.
I cant understand why this doenst emit or receive the message.
At angular I got this error:
http://localhost:4200/socket.io/?EIO=3&transport=polling&t=NEhK-JT 404
4200 is the PORT of my Angular Application and 8080 from my server.
NodeJS:
// Define Porta
const port = process.env.PORT || 8080;
var server = app.listen(port, function () {
console.log('Server Online - ' + port);
});
// Socket.io
var io = require('socket.io').listen(server);
io.on('connect', function (socket) {
console.log(`${socket.id} is connected`);
socket.on('room', function (room) {
console.log('room', room)
socket.join(room);
});
});
Angular 9:
import * as io from 'socket.io-client'
public socket
public orgId: string = '123abc'
ngOnInit(): void {
this.setupSocketConnection();
}
chat(nome: string, avatar: number, mensagem: string) {
io.connect(this.orgId).emit('organizacao', {
nome: nome,
mensagem: mensagem,
avatar: avatar,
});
}
setupSocketConnection() {
this.socket = io.connect(`http://localhost:8080`, {
reconnectionDelay: 1000,
reconnection: true,
reconnectionAttempts: 10,
transports: ['websocket'],
agent: false,
upgrade: false,
rejectUnauthorized: false
});
}
From my Console.log at Server
zEnR7Cp23zcur4_kAAAH is connected
86sIiMA8vRZEN-WcAAAI is connected
SU4K2n9jAx_UO2ndAAAJ is connected
UAwlMpNiZWhw_eo9AAAK is connected
K6myruVum4FPKTeLAAAL is connected
Z5QULdZtdsRo5gC1AAAM is connected
If you are trying to implement rooms then please read https://socket.io/docs/rooms/
For your case what I can see here is that on server-side you are listening to the event named room and on client-side you are emitting to organizacao and also you need to use the socket object instead of io, refer from here https://www.npmjs.com/package/socket.io-client
socket.connect(this.orgId).emit('room', {
nome: nome,
mensagem: mensagem,
avatar: avatar,
});

Knex leaves open server when using Jest (recommendation)

I'm trying to do some TDD with my following stack
Jest
Node
Koa2
SuperTest
Knex
Objection
My problem starts with the open handler of the koa server and I could solve that with the instance of the server and close it with server.close()
However, I have the same problem with knex; It leaves the server open and I have to run the knex.close to stop it. With that i can avoid the following error message
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't >stopped in your tests. Consider running Jest with --detectOpenHandles to >troubleshoot this issue.
knex.config
const config = {
development: {
client: 'pg',
connection: process.env.DATABASE_URL,
migrations:{
directory:"./migrations/"
},
pool: { min: 0, max: 7 }
},
test: {
client: 'pg',
connection: process.env.DATABASE_URL,
migrations:{
directory:"./migrations/"
},
pool: { min: 0, max: 7 }
},
//TBD
staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},
//TBD
production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}
}
module.exports = config;
user.model.js
'use strict';
const knex = require('../config/db/knex');
const { Model } = require('objection');
Model.knex(knex);
class User extends Model {
// Table name is the only required property.
static get tableName() {
return 'user';
}
// Custom function to close knex
static close() {
knex.destroy();
}
}
module.exports = User;
user.test.js
const supertest = require('supertest');
const server = require('../../server');
var request = require("supertest").agent(server);
describe("Test users routes", () => {
let Model;
beforeAll(async () => {
// do something before anything else runs
console.log('Jest starting!');
Model = require('../../models/user.model')
});
// close the server after each test
afterAll(() => {
server.close();
Model.close();
console.log('server closed!');
});
test("Get /",async () => {
let res = await request.get('/users/');
expect(res.status).toBe(200);
});
});
I'm pretty sure it could be a better approach solution for what I did, maybe something related with the pool or some callback on the knex.cofing but I'm not sure.
Thank you

Trying to connect to SQL server using Tedious Connection and Windows Authentication?

I'm trying to connect to SQL server using tedious connection pool and windows authentication. But I get an error:
message: 'Login failed for user \'\'.', code: 'ELOGIN'
I'm not sure what I'm doing wrong. I'm using the latest version of tedious.
.env file
SQL_SERVER=localhost
SQL_UNAME=Username
SQL_PSWD=Password
SQL_DB=DatabaseName
SQL_DOMAIN=US
dbController.js
const {Request} = require('tedious');
const TYPES = require('tedious').TYPES;
const ConnectionPool = require('tedious-connection-pool');
const dbConfig = require('./dbconfig');
const poolConfig = {
min: 1,
max: 1,
log: true
};
let _rows = [];
const pool = new ConnectionPool(poolConfig, dbConfig);
pool.on('error', (err) => {
console.log(err);
});
dbConfig.js
const dotenv = require('dotenv');
dotenv.config();
module.exports = {
server: process.env.SQL_SERVER,
options: {
instanceName: 'SQLEXPRESS',
encrypt: false,
database: process.env.SQL_DB,
rowCollectionOnDone: true,
useColumnNames: true
},
authentication: {
type: 'ntlm',
options: {
userName: process.env.SQL_UNAME,
password: process.env.SQL_PSWD,
domain: process.env.SQL_DOMAIN
}
}
};
The problem is tedious-connection-pool is using tedious version 1 instead of tedious version 9.
I'm hoping to solve that with tedious-connection-pool2 based on a PR I found that never got merged upstream.
So, wait a day, and find my tedious-connection-pool2 and use the overrideTedious option that should work.

Categories