Reputation: 306
I am having problems with redis, the documentation is not being clear to me and all my efforts to try to make a simple stack has gone awry.
I use nodejs
as a server andredis
together with socket
I need to make a list of a specific type of users the relation between them and the rooms is many to many, like this:
MEMBERS <-----> ROOMS
Several members may be in several rooms, and several rooms may have several members.
All this information was being allocated in memory, it brought a lot of unnecessary weight, hence the idea of using redis to manage these types of connections.
In my code I'm doing this:
Start the service like this:
const io = require ('socket.io'). listen (http);
const redisSocket = require ('socket.io-redis');
const redis = require ("redis");
I get the connections in the socket
:
io.sockets.on ('connection', (socket) => {my _functions});
And I get in a socket.on
the members I need to work with
socket.on ('ioUpdateLocation', (data) => {
// Send the location to the devices in the room
io.sockets.to (data.room) .emit ('ioReceiveLocation', date);
addRoom (data.room, socket.id);
// Emits the location for line X destination Y
io.sockets.to (data.room + data.destiny) .emit ('ioReceiveLocation', data);
addRoom (data.room + data.destiny, socket.id);
});
And so I plan to put that socket in the room like this:
const addRoom = (room, socketId) => {
console.log ('room% s socket% s', room, socketId);
redisClient.lpush (room, socketId, function (err, reply) {
console.log ('err', err); // prints 2
console.log ('reply', reply); // prints 2
});
}
My idea is like this:
ROOMS | MEMBERS
123 | ['member01', 'member02', 'member03']
123A | ['member01']
So using redis
I hope toadd
and remove
members from a room, andcheck
if that member is in a room for example X.
BUSINESS RULES
With the attempts was made:
1- client.set ();
2- client.hmset ();
3- client.rpush ();
4- client.sadd ();
None successfully. At first it replaced the value in the example would look like this (the same values were inserted in the following order member01, member02, member03):
ROOMS | MEMBERS
123 | 'member04'
123A | 'member01'
In attempts 2,3,4 the result was an error:
{ReplyError: WRONGTYPE Operation against a key holding the wrong kind of value at new Command (/var/www/lb-api-geolocation/node_modules/redis/lib/command.js:12:22) at RedisClient.lpush (/var/www/lb-api-geolocation/node_modules/redis/lib/commands.js:58:47) at addRoom (/var/www/lb-api-geolocation/src/socket/connect.js:144:17) at Socket. (/var/www/lb-api-geolocation/src/socket/connect.js:94:9) at emitOne (events.js: 115: 13) at Socket.emit (events.js: 210: 7) at /var/www/lb-api-geolocation/node_modules/socket.io/lib/socket.js:513:12 at _combinedTickCallback (internal / process / next_tick.js: 131: 7) at process._tickDomainCallback (internal / process / next_tick.js: 218: 9) command: 'RPUSH', args: ['59c54ace9e450c004ad4c90etripA', 'E4NXUZ417M2UwUxDAAAC'], code: 'WRONGTYPE'}
How to solve this?
Upvotes: 1
Views: 226
Reputation: 3998
you error comes directly from redis and the problem is that the key you are trying to do LPUSH
already exists and has a different type.
An introduction to Redis data types and abstractions
Example why you got WRONGTYPE
error:
> set foo bar
OK
> lpush foo 1 2 3
(error) WRONGTYPE Operation against a key holding the wrong kind of value
> type foo
string
I suggest that you open a redis-shell and do a FLUSHALL
to clean you databases
Upvotes: 1