I have redis up and running on port 6379, connecting via telnet works fine.
I'm trying to connect to it on node.js, but i'm getting no response from the event listeners.
If i make a call any function like client.set() I get: "ClientClosedError: The client is closed".
This is the code im running:
const redis = require('redis');
const client = redis.createClient(6379);
client.on('connect', () => {
console.log('connected');
});
client.on('end', () => {
console.log('disconnected');
});
client.on('reconnecting', () => {
console.log('reconnecting');
});
client.on('error', (err) => {
console.log('error', { err });
});
setTimeout(() => {console.log("goodbye") }, 20*1000 );
Nothing happens for 20 seconds, and then it closes
Starting from v4 of node-redis library, you need to call client.connect() after initializing a client. See this migration guide.
const redis = require('redis');
const client = redis.createClient({ socket: { port: 6379 } });
client.connect();
client.on('connect', () => {
console.log('connected');
});
You might also want to consider running the client connect method with await in an asynchronous function. So you don't have to worry about event listeners.
const redis = require('redis');
(async () => {
try {
const client = redis.createClient({ socket: { port: 6379 } });
await client.connect();
console.log('connected');
} catch (err) {
console.error(err)
}
})()
Related
I am using nodejs to run the server, there is no log file
This is my server.js
const https = require('https');
const fs = require('fs');
const ws = require('ws');
const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
const wss = new ws.Server({noServer: true});
function accept(req, res) {
// all incoming requests must be websockets
if (!req.headers.upgrade || req.headers.upgrade.toLowerCase() != 'websocket') {
res.end();
return;
}
// can be Connection: keep-alive, Upgrade
if (!req.headers.connection.match(/\bupgrade\b/i)) {
res.end();
return;
}
wss.handleUpgrade(req, req.socket, Buffer.alloc(0), onConnect);
}
function onConnect(ws) {
ws.on('message', function (message) {
let name = message.match(/([\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_C}]+)$/gu) || "Guest";
ws.send(`${name}!`);
//setTimeout(() => ws.close(1000, "Bye!"), 5000);
});
}
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
This is my code in react
componentDidMount() {
var connection = new WebSocket('wss://localhost:8000/');
connection.onopen = function(e) {
connection.send("add people");
};
connection.onmessage = function(event) {
// alert(`[message] Data received from server: ${event.data}`);
console.log("output ", event.data);
};
}
While I am trying to connect with web-socket with my jsx file its give me an error which is Firefox can’t establish a connection to the server at wss://localhost:8000/.
Your implementaion needs some changes. In the backend server, you forgot to call the onConnect function. So your ws.on method will never call.
Also, you imported the ws and create a WebSocket server wss, but you add some event listener on ws wrongly, you should add listener on your Websocket instance (wss):
// rest of the codes ...
const was = new ws.Server({noServer: true})
wss.on('connection`) {
// do something here ...
}
// rest of the codes ...
https.createServer(options, () => {
// do something here ...
})
There are some examples of how to create the WebSocket server along with the HTTP server on ws npm page.
I have the following module which connects to a Redis database, and I want to get the client instance so I can call it from other modules without creating a new instance each time, I did the following:
let client;
const setClient = ({ redis, config }) => {
client = redis.createClient({
host: config.redis.host,
port: config.redis.port
});
};
const getClient = () => {
return client;
};
const connect = ({ redis, config, logger }) => {
setClient({ redis, config });
client.on('connect', () => {
logger.info(`Redis connected on port: ${client?.options?.port}`);
});
client.on('error', err => {
logger.error(`500 - Could not connect to Redis: ${err}`);
});
};
module.exports = { connect, client: getClient() };
when i call the client from other modules using const { client } = require('./cache'); it gives me undefined
erase letClient() from top(the let) and on the bottom add const client = getClient() and on module exports just go with client instead of client: getClient()
I come up with the following solution:
const cacheClient = () => {
return {
client: undefined,
setClient({ redis, config }) {
client = redis.createClient({
host: config.redis.host,
port: config.redis.port
});
},
getClient() {
return client;
},
connect({ redis, config, logger }) {
this.setClient({ redis, config });
client.on('connect', () => {
logger.info(`Redis connected on port: ${client?.options?.port}`);
});
client.on('error', err => {
logger.error(`500 - Could not connect to Redis: ${err}`);
});
}
};
};
module.exports = cacheClient;
If there's a better approach please let me know.
I am working on an app that involves the use of WebSockets. I have the following code as a part of the application:
io.on('connection', socket => {
let PLAYER = {};
// Listener for event 1
// Listener for event 2
// ...
socket.on('setName', ({name, role, room}) => {
PLAYER.name = name;
PLAYER.role = role;
PLAYER.room = room;
try{
joinRoom(PLAYER); // This function can throw an error
socket.emit('roomJoin');
}catch(e){
socket.emit('error', e.message);
return;
}
});
});
Now, when the joinRoom function does throw an error, Node just crashes with the following exception:
events.js:306
throw err; // Unhandled 'error' event
^
Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('error message')
The error event is never emitted. What has made the try catch block fail?
Note: I am using TypeScript, but have removed type declarations in the above snippet as they shouldn't have anything to do with the problem.
Edit 1:
I have recreated the issue with simpler code as follows:
import express from 'express';
import socketio from 'socket.io';
import http from 'http';
const PORT = process.env.PORT || 8000;
const app = express();
const server = http.createServer(app);
const io = socketio(server);
server.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
const checkLength = (bar) => {
if(bar.length > 14){
throw Error('word is too long!');
}
// ...
}
io.on('connection', socket => {
console.log('new socket connection');
socket.on('foo', ({bar}) => {
try{
checkLength(bar);
}catch(e){
socket.emit('error', e.message);
return;
}
console.log('app did not crash!');
});
}
When the length of bar is greater than 14, the app crashes with the same error mentioned above.
After replicating the server on my local machine, I realized you were calling emit on a reserved keyword for socket.io. When you call socket.emit('error', e.message), there is an expected listener to exist. One like this:
socket.on('error', err => {
// An error occurred.
})
You should use a different phrase for your emit. I personally use oops on my socket servers. socket.emit('oops', {error}). You can read more about error handling on their page.
To add, socket.emit('error') does not pass the error or any data down to the client and instead is handled by the server, which is why you need to use a different phrase.
You can use the promisses, and than you can profite from the then() method which also returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.
So your code should be something like this:
import express from 'express';
import socketio from 'socket.io';
import http from 'http';
const PORT = process.env.PORT || 8000;
const app = express();
const server = http.createServer(app);
const io = socketio(server);
server.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
const checkLength = (bar) => {
return new Promise((resolve, reject)=>{
if(bar.length > 14){
// throw Error('word is too long!');
reject('word is too long!')
} else {
resolve('app did not crash!')
}
});
}
io.on('connection', socket => {
console.log('new socket connection');
socket.on('foo', ({bar}) => {
checkLength(bar).then((result) => {
console.log('app did not crash!');
}).catch(err => {
console.log("app did crash with this error:", err);
});
});
}
I noticed you hadn't initialized your PLAYER variable.
let PLAYER = {};
#EDIT 1:
try also adding an error handler for your socket server and log it to get an idea of what is causing the error.
socket.on('error', (error) => {
console.log(error);
});
I try to test socket.io using mocha, but i have a problem when make a socket call on. I think socket is not connected, because the console log not triggered but the console.log outside the socket call is show. Or it's wrong about how i use the done() method ?
const { assert } = require('chai');
const ioServer = require('socket.io');
const ioClient = require('socket.io-client');
const http = require('http');
const socketUrl = 'http://localhost:5000';
const options = {
transports: ['websocket'],
'force new connection': true,
};
let server;
let client;
let httpServer;
describe('Socket', () => {
beforeEach(done => {
httpServer = http.createServer().listen();
server = ioServer(httpServer);
client = ioClient.connect(socketUrl, options);
server.on('connection', socket => {
console.log('connected server');
socket.emit('echo', 'Hello World');
});
client.on('echo', message => {
console.log('connected client');
console.log(message);
});
console.log('test');
done();
});
afterEach(done => {
server.close();
client.close();
done();
});
it('It should connect socket', done => {
client.on('echo', message => {
assert.equal(message, 'Hello World');
});
done();
});
});
Here's the screenshoot cli terminal when running mocha.
Mocha
I am trying to create some unit tests with Mocha and Chai but I am unable to connect to the mysql server. Here I have my unit test:
process.env.NODE_ENV = 'test';
let chai = require('chai');
let chaiHttp = require('chai-http');
let server = require('../app');
let should = chai.should();
chai.use(chaiHttp);
describe('API endpoint /authenticate', function () {
before((done) => {
/* Todo: Clean up Database*/
done()
});
it('should authenticate a user given CORRECT username/password', (done) => {
let user = {
username: 'ax850',
password: 'test'
};
chai.request(server)
.post('/api/authenticate')
.send(user)
.end((err, res) => {
res.should.have.status(200);
done();
});
done();
});
});
As you can see, I am doing a POST request. Then in the script where I handle this POST request, I am connecting to a mySQL server like so:
const db = require('./../../db_conn');
And in the db_conn I have:
const mysql = require('mysql');
const config = require('config.json')('./config.json');
let db;
const db_config = config.a_c.dbConfig;
function connectDatabase() {
if (!db) {
db = mysql.createConnection({
host: db_config.host,
user: db_config.user,
port: db_config.port,
database: process.env.NODE_ENV === 'test' ? db_config.db_test : db_config.db_dev,
//database: db_config.db_dev,
password: db_config.password
});
db.connect(function(err){
if(!err) {
console.log('Database is connected!');
} else {
console.log('Error connecting database!');
}
});
}
return db;
}
module.exports = connectDatabase();
However, when I run my test command: mocha --timeout 10000, it doesn't connect to the database. The mysql.createConnection runs unsuccessfully but doesn't give an error either. Any ideas? This works when I run the server without testing.
You are calling done before the request finishes.
chai.request(server)
.post('/api/authenticate')
.send(user)
.end((err, res) => {
res.should.have.status(200);
done();
});
// this is executed before chai.request is fulfilled
done();