I have a function which I will call from time to time.
function blah() {
sub.unsubscribe();
sub.subscribe("a");
sub.subscribe("b");
}
Above results in error message "Error: node_redis command queue state error."
Same as https://github.com/mranney/node_redis/issues/137
Once a connection is subscribed, it can only issue subscription related commands (subscribe, psubscribe, etc)
Might another part of your code be using the same connection?
You could also try and subscribe to multiple channels with one call sub.subscribe("a", "b") or subscribe to a pattern that matches your need?
I am currently using redis.I was also facing the same issue in which i was trying to subscribe a set of channels.
var redis = require('redis')
const subscribe = redis.createClient({
host: 'localhost',
port: 6379
})
subscribe.psubscribe(`user:chat:*`)
subscribe.on('pmessage', function(pattern, channel, message) {
console.log(channel, message, pattern)
// Write Your Awesome Code here.
})
Psubscribe used for subscribing for multiple channels using redis. You can subscribe multiple channel on the basis of pattern.
Related
I have one redis client for pub-sub. I'm using a websocket message handler to dynamically subscribe to a redis channel. The payload of the websocket message contains an ID that I use to create the channel-name. So for example lobby:${lobbyID}:joined.
Subscribing to this channel works fine, messages are received when publishing to that channel.
But the issue that I'm having is that I want to unsubscribe from this channel at one point. My assumption by reading the redis-documentation is that I would use punsubscribe so I can unsubscribe from any channels with the pattern lobby:*:joined, but messages are still received after trying that.
import redis from 'redis';
const subClient = redis.createClient();
subClient.on('message', (channel, message) => {
// Received message x on channel y
});
const socketHandlerSubscribe = (lobbyID) => {
subClient.subscribe(`lobby:${lobbyID}:joined`);
}
const socketHandlerUnsubscribe = () => {
subClient.punsubscribe('lobby:*:joined'); // true
}
When using the redis-cli the pattern seems valid when using PUBSUB CHANNEL lobby:*:joined. I could solve this issue by passing a lobby ID to the unsubscribe handler aswell, but punsubscribe should be the solution for it.
I also encountered this earlier with a scenario where I looped through an array of user ID's and created a subscription for each on statuses:${userID} and tried a punsubscribe on statuses:*, without any success.
Am I doing something wrong or this is an issue node-redis related? I'm using redis version 2.8.0
I noticed that there are two different types of subscriptions. On channels and patterns. In my question I was subscribing to a channel, and unsubscribing on a pattern, these two are not 'compatible' so this won't work.
I used nc to debug this, as redis-cli won't allow additional commands when entering subscribed state.
I have a broadcast server with socket.io and ioredis via node.
However, with my current form I can only subscribe to one channel a time.
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('mychannel');
Considering that I must have innumerable channels (one for each registered user, for instance) I cannot hard type every channel on the node server.
I've tried also redis.subscribe('*') but without success.
Any light?
Using redis.psubscribe('*') and redis.on('pmessage', handlerFunction) did the trick.
I want to find out what are all the avaliable channels (and subscribers) in Redis Pub/Sub.
I want to do it so I could build a ui to display the current pub/sub status.
From looking on the redis and ioredis packages I couldn't find anything...
Any ideas will be helpful,
Thanks!
The redis client exposes all Redis commands, including PUBSUB CHANNELS:
client.pubsub('channels', (err, channels) => {
if (err) {
...
} else {
console.log('Channels:', channels); // array
}
});
I don't think it exposes the subscribers, but you can call PUBSUB NUMSUB in a similar way to retrieve the number of subscribers for each channel.
I am having trouble figuring out how to subscribe to a particularly "channel" with ZMQ with regard to its pub/sub functionality.
Here is the publisher:
var zmq = require('zmq');
var pub = zmq.socket('pub');
pub.bindSync('tcp://127.0.0.1:5555');
setInterval(function(){
pub.send('pub msg');
},500);
here is the subscriber:
var sub = zmq.socket('sub');
sub.connect('tcp://127.0.0.1:5555');
sub.subscribe(''); //herein lies the question
sub.on('message',function(msg){
console.log('Received msg:',msg);
}
This works as is, but the problem is that if I change the argument to sub.subscribe to anything but an empty string (''), the subscriber doesn't receive any messages from the publisher.
How do I configure pub/sub with ZMQ correctly?
sub.subscribe('topic') adds a filter to your subscriber socket so that you only receive messages starting with the string topic. You can add multiple filters by calling it more than once. sub.subscribe('') removes any existing filter so your subscriber gets all messages sent by the publisher.
In your code using sub.subscribe('pub') would yield messages on the subscriber side.
The pub/sub example in the zeromq.node GitHub is a good place to look to understand how subscriptions work.
I'm using amqplib with node.js and I'm trying to make sure I understand the concept of channels.
This is from the amqplib documentation: Channels are multiplexed over connections, and represent something like a session, in that most operations (and thereby most errors) are scoped to channels.
Here is some basic code where I'll open a amqp connection, create a channel, an exchange and a queue:
var amqp = require('amqp/callback_api');
var connection = amqp.createConnection({ host: "localhost", port: 5672 });
connection.on('ready', function () {
connection.createChannel(function(err, ch) {
ch.assertExchange('1', 'fanout', function(err, ok) {});
ch.assertQueue('a', {
exclusive: true,
durable: true
}, function(err, ok) {
});
});
In the above code do exchange '1' and queue 'a' only exist on the channel for which they were defined? By this I mean, if I were to publish a messages to exchange a from another channel would exchange a still route the messege?
All entities like exchanges, queues, messages exists globally on the broker and visible to all connections and channels inside single vhost. There are no exceptions from that.
Queues may be defined as exclusive, then they are exists only within same connection and when it closed they are destroyed. This is special case while as they still visible, they are not accessible from other connections.
There are auto-delete option for both queues and exchanges, which is by default set to true. It means that they will be removed after usage (see exchange and queue auto-delete docs for details).