Get the App
SLTechnology News&Howtos  ›  Database  › 

Publish and subscribe to Redis

Shulou Source: shulou.com Published: 2022-06-01 06:44:55 10月25日 Update

Subscribers can subscribe to the message types they are interested in from redis server through the subscribe and psubscribe commands. Redis refers to the information type as a channel. When the publisher sends a specific type of information to the redis server through the publish command, the full client that subscribes to that information type will receive this message.

Session 1: monitoring channel 1

127.0.0.1 purl 6379 > subscribe tv1

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "tv1"

3) (integer) 1

Session 2: monitoring channels 1 and 2

127.0.0.1 purl 6379 > subscribe tv1 tv2

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "tv1"

3) (integer) 1

1) "subscribe"

2) "tv2"

3) (integer) 2

Session 3: post information on channel 1

127.0.0.1 purl 6379 > publish tv1 stone

(integer) 2

View session 1:

127.0.0.1 purl 6379 > subscribe tv1

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "tv1"

3) (integer) 1

1) "message"

2) "tv1"

3) "stone"

View session 2:

127.0.0.1 purl 6379 > subscribe tv1 tv2

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "tv1"

3) (integer) 1

1) "subscribe"

2) "tv2"

3) (integer) 2

1) "message"

2) "tv1"

3) "stone"

Session 3: post information on channel 2

127.0.0.1 purl 6379 > publish tv2 stone2

(integer) 1

View session 2:

127.0.0.1 purl 6379 > subscribe tv1 tv2

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "tv1"

3) (integer) 1

1) "subscribe"

2) "tv2"

3) (integer) 2

1) "message"

2) "tv1"

3) "stone"

1) "message"

2) "tv2"

3) "stone2

Tags: Information type channel subscription command message monitor interest publisher available through subscriber channel complete Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Redmi Linux NVidia MariaDB