Git Product home page Git Product logo

Comments (2)

mranney avatar mranney commented on September 18, 2024

Fixed by you in dcbb87d

from node-redis.

georgeciobanu avatar georgeciobanu commented on September 18, 2024

This still occurs in the following situation:
Start redis-server
run node client.js (see file below)
restart redis-server

The client remembers it is in subscribe mode and somehow thinks the connect attempt is a command?
127.0.0.1:6379 failed - connect ECONNREFUSED
Some error happened to the redis client: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
Connected

/Users/george/code/touchPals/touchNode/node_modules/redis/index.js:680
throw new Error("Connection in pub/sub mode, only pub/sub commands may
^
Error: Connection in pub/sub mode, only pub/sub commands may be used
at RedisClient.send_command (/Users/george/code/touchPals/touchNode/node_modules/redis/index.js:680:15)
at RedisClient. (/Users/george/code/touchPals/touchNode/node_modules/redis/index.js:826:25)
at RedisClient.ready_check (/Users/george/code/touchPals/touchNode/node_modules/redis/index.js:340:10)
at RedisClient.on_connect (/Users/george/code/touchPals/touchNode/node_modules/redis/index.js:216:18)
at Socket. (/Users/george/code/touchPals/touchNode/node_modules/redis/index.js:66:14)
at Socket.emit (events.js:64:17)
at Object.afterConnect as oncomplete

Test client:

var fs = require('fs');

var options = {
key: fs.readFileSync('../certificates/arranged_marriage.key'),
cert: fs.readFileSync('../certificates/arranged_marriage.crt')
};

console.log("Things are going well");
var app = require('https').createServer(options);

app.listen(8000);
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({server: app});

// var WebSocketServer = require('ws').Server,
// wss = new WebSocketServer({port:8000, host:'localhost'});
var socketByToken = {};

// Good to know about this potential bug - it occurs sometimes when
// the redis server is down and the client tries to reconnect before a certain number of tries
// #20
var redis = require("redis"),
redisClient = redis.createClient();

wss.on('connection', function(ws) {

console.log("Server: someone connected to me");
// console.log('additional info:');
// console.log(ws);
ws.on('message', function(token) {
    console.log('received token: %s', token);
    // Extract token from message
    socketByToken[token] = this;
});
ws.on('error', function(error) {
    console.log('some error happened:' + error);
    console.log('A client may have called Close');
});

});

redisClient.on("message", function (channel, jsonMessage) {
console.log("Received message:");
console.log(jsonMessage);

// This is a potential security issue - JSON objects can contain code
var parsedMessage = "";
try {
parsedMessage = JSON.parse(jsonMessage);
var token = parsedMessage.token;
delete parsedMessage.token;
console.log("About to send command: " + JSON.stringify(parsedMessage) + " to user with token: " + token);
socketByToken[token].send(JSON.stringify(parsedMessage));

} catch (err) {
console.log(err);
return;
}

console.log("Sent message: " + parsedMessage + " to user with token: " + token);
});

redisClient.on("error", function (error) {
console.log("Some error happened to the redis client: " + error);
});

redisClient.on("connect", function (maybe) {
console.log("Connected");
});

redisClient.subscribe("chats");
console.log("Connected to Redis...");

wss.on('close', function(ws) {
console.log("Server: someone disconnected to me");
});

from node-redis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.