Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Bugfix: The websocket server is registering for every device the 'acc…
Browse files Browse the repository at this point in the history
…ountId/deviceId' channel at redis.

Unfortunatley, Redis is sending all broadcast to all instances. So if for instance two devices are registered, both routines get the request to send the command. This leads to duplications
of command messages. This patch adds filtering to avoid duplication of several messages and only the addressed subroutine is answering.

Signed-off-by: Marcel Wagner <[email protected]>
  • Loading branch information
wagmarcel authored and oguzcankirmemis committed Jul 19, 2020
1 parent 4b29a45 commit 7d0a3c9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ var config = {
winston.format.timestamp(),
winston.format.printf(info => { return `${info.timestamp}-${info.level}: ${info.message}`; })
),
transports : [new winston.transports.Console()]
transports : [new winston.transports.Console()],
level: process.env.DEBUG || "info"
}
};

Expand Down
5 changes: 5 additions & 0 deletions iot-entities/redis/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ function RedisClient(conf) {
}
};

me.unsubscribe = function(channel) {

me.client.unsubscribe(channel);
};

me.onMessage = function(callback) {
if ( callback ) {
me.client.on('message', function (channel, message) {
Expand Down
3 changes: 2 additions & 1 deletion lib/logger/winstonLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ var winston = require('winston'),

module.exports = winston.createLogger({
format: loggerConf.format,
transports: loggerConf.transports
transports: loggerConf.transports,
level: loggerConf.level
});
11 changes: 10 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ wsServer.on('request', function(request) {

redisClient.subscribe(channel);
redisClient.onMessage(function (channel, message) {
logger.info('Receiving Redis message for ' + channel + ' channel');
// nodejs pubsub redis client is not filtering the messages by channels.
// That means: Every message arrives here, independent of the registered channel.
// We have to check whether the message is meant for us
if (message === undefined || message.body === undefined || message.body.content === undefined ||
message.body.content.domainId === undefined || message.body.content.domainId !== accountId){
return;
}
logger.info('Receiving Redis message for ' + channel + ' channel');
if (message.type === 'actuation') {
if (message.credentials.username === conf.ws.username &&
message.credentials.password === conf.ws.password) {
Expand Down Expand Up @@ -140,8 +147,10 @@ wsServer.on('request', function(request) {
});

connection.on('close', function(reasonCode, description) {
logger.info("Disconnected from client ");
Object.keys(clients).some(function(channel) {
if(clients[channel] === connection) {
redisClient.unsubscribe(channel);
delete clients[channel];
return true;
}
Expand Down

0 comments on commit 7d0a3c9

Please sign in to comment.