#Redis pattern pub/sub questions

1 messages ยท Page 1 of 1 (latest)

indigo bay
#

So to start with

#

This is how i initialize the lib, my goal is next:

  1. If you dont tell the servers property, it will use the channel pattern <channel>:global as defauult
  2. In case you give any server via the servers property, it will subscribe to the next pattern <channel>:<server>
// Redis settings
RedisInfo builder = RedisInfo.builder()
  .uri("") // Redis connection uri
  .server("proxy-1") // Current server name
  .servers("skywars") // All registered servers
  .build();

// Creating api instance
RedisAPI redis = new RedisHandler(builder);
redis.connect();

// Creating broker instance 
RedisBroker broker = redis.newBroker("plugin-sync"); // We pass the channel we want to use

// Registering listener(s)
broker.getEvents().register(new NetworkListener());

// Registering packet(s)
broker.getPackets().register(new UserPacket());
broker.getPackets().register(new TextPacket());

// Sending packets
broker.publish(packet); // Publish to pattern <channel>:global
broker.publish(packet, server); // Publish to pattern <channel>:<server>
#

So at the momment i have the next code:

public class RedisListener extends JedisPubSub {

  private final BrokerService broker;

  public RedisListener(BrokerService broker) {
    this.broker = broker;
  }

  @Override
  public void onPMessage(String pattern, String channel, String data) {
    if (!channel.equals(broker.getChannel())) return;
    Payload payload = broker.getRedis().getInfo().getGson().fromJson(data, Payload.class);
    if (payload == null) throw new RedisException("[Redis] An invalid payload was received");
    JsonObject packet = broker.getRedis().getInfo().getGson().fromJson(payload.getPacket(), JsonElement.class).getAsJsonObject();
    System.out.println("[Jedis-Listener] Received channel=" + channel + " pattern=" + pattern + " packet=" + packet);
}
supple ore
#

Which is the question?

indigo bay
#

So far im sending a packet

#

But the sout() is never sent

#

Debug capture from the packet sender

#

Debug capture from the receiver, if you the data never is received

supple ore
#

How/Where are you sending the data?

indigo bay
#

Right

#

1m

#

This how im subscrbing, a subscription into global channel is always done + the servers you input into the settings

#

Global publisher

#

Server publisher (specific server)

indigo bay
supple ore
#

Are those debug messages being sent?

#

I guess yes

indigo bay
#

At the moment none of the publishers data is sent

supple ore
#

Which Redis dependency are you using? So I can see docs

indigo bay
#

Jedis

#

I will lok the exact version

#

Jedis 4.3.1

#

And to listen for data im using JedisPubSub with pattern

#

Really thanks man, i have been many hours with this. Before implenting the patters i was using normal onMessahe() method but the data wasnt bein received correctly

#

I wont lie im really stressed and feeling really idiot because i cant implement such a simple thing ๐Ÿ˜ฎโ€๐Ÿ’จ

supple ore
indigo bay
#

Yes, instead of subscribe im using the psubscribe whcih allow you to use patterns

indigo bay
supple ore
#

Let's try something, subscribe to a "dummy" channel, and try to send data into that dummy channel

#

Instead of using psubscribe

indigo bay
#

Yes i did that before

#

And was working perfect

#

Just having problems to implement my logic having 1 global channel for all servers and an individual channel per server

supple ore
#

Can I see your psubscribe argument?

indigo bay
#

yes no problem

supple ore
#

Also try debugging what is being parsed as the channel argument, maybe it's not the same

indigo bay
#
public BrokerService(RedisAPI redis, String channel) {
  this.redis = redis;
  this.channel = channel;
  this.events = new EventsManager(redis.getInfo().getExecutor());
  this.packets = new PacketsManager();
  String[] channels = Collections.add(Stream.of(redis.getInfo().getServers()).map(server -> server = channel + ":" + server).toArray(String[]::new), channel + ":global");
  redis.getInfo().getExecutor().execute(() -> redis.getClient().getResource().psubscribe(new RedisListener(this), channels));
  System.out.println("[RedisBroker] Subscribed to channels=" + Arrays.toString(channels));
}```
#

Collections#add is a custom method if you are asking, you give String[] input, then the value is add to the input and return back

supple ore
#

Maybe the sender is not actually able to send data, because he's not subscribed to the actual channel. Let me explain.
According to these debugs:

Which I'll guess:
#1 = BungeeCord
#2 = SkyWars

Maybe to send data to plugin-sync:skywars to the SkyWars server, the SkyWars server must be subscribed too into that channel (just as an example)
Basically, both debugs should contain at least 2 same channels (the one for the server, and the one global)

indigo bay
#

oh okay

supple ore
#

I guess you are sending though plugin-sync:skywars

#

Pretty sure if you switch to global, It may work

indigo bay
#

okay, and how would do then, because the data wil be self listened ?

#

Talking individual server channel

supple ore
#

Try it and see what happens

indigo bay
#

right

#

Now i make the global to work

supple ore
#

But I don't think Redis would send the client who send data the message with the data he just sent

#

In the worst case, you will see a message received on both clients

indigo bay
supple ore
#

Did it work?

indigo bay
#

1m

#

I will subscribe the server itself

#

So each client, must be subscribed to global channel and individual channel right?

supple ore
#

At least

indigo bay
#

Perfect ill modify code and test it

supple ore
#

So each client is able to listen on his self channel, and in the global one

indigo bay
#

right, so from redis xp the client self listen the data he sent, so there i would add some checks

#

Because my payload is like this sender (string), receiver (string) and packet (json)

supple ore
indigo bay
#

Atm i havent test tho

#

I will test once i finish an utility class for subscription

indigo bay
#

๐Ÿ˜‚

#

Lamo shity redis

supple ore
#

This might help you

indigo bay
#

Oh really thanks man