简体   繁体   中英

Node.js redis pubsub

I'm really beginner of nodejs. I want to make a chatting service using nodejs. I use nodejs/jade/mysql to construct basic part of my system and now I want to provide pub/sub to users.

We receive users' interests from text field or using hash tags (anyway we received users' interests and stored in MySQL -> we did it). Then, we want to show users chatting room list according to their interests. For instance A's interests are 'game', 'car' and 'food', then we search chat rooms with 'game', 'car', 'food' and show A these chat rooms first.

I want to use redis to provide this service but i really have no idea!

1) I installed redis and can run redis-server.
2)

 //redis var redis = require('redis'); var publisher = redis.createClient(); var subscriber = redis.createClient(); subscriber.on('message', function(channel, message){ console.log('Message ' + message + ' on channel ' + channel + ' arrived!'); }); subscriber.on('subscribe', function(channel){ publisher.publish('test', 'the a team'); publisher.publish('test', 'the b team'); }) subscriber.subscribe('test');

This is short code that I tried to understand redis.

3) I don't know how can I read data stored in Mysql and show users chat room according to their interests using redis.

Redis is a advanced key-value cache and store.Its operations cannot be directly mapped to mysql.

In redis you can set either key value pair or a hash under a key. That is : If you want to store your name in redis it can be done by:

 var client = redis.createClient(); client.set("name", "John")

Retrieve the values using client.get("name")

Similarly under a single key you can store multiple key value pairs, as hash. That under a name if you want to store their details like age, place, company etc.Then hash should be used. Redis has method "hmset" and "hmget" for hash opertaions.

In redis like in cache you can set expiry time.

There are different method available. You can explore those. For reference http://redis.io/commands

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM