Code Monkey home page Code Monkey logo

redis-subscriber's Introduction

Mix Redis Subscriber

Redis native protocol Subscriber based on Swoole coroutine

基于 Swoole 协程的 Redis 原生协议订阅库

使用 Socket 直接连接 Redis 服务器,不依赖 phpredis 扩展,该订阅器有如下优点:

  • 平滑修改:可随时增加、取消订阅通道,实现无缝切换通道的需求。
  • 跨协程安全关闭:可在任意时刻关闭订阅。
  • 通道获取消息:该库封装风格参考 golang 语言 go-redis 库封装,通过 channel 获取订阅的消息。

Installation

  • Swoole >= 4.4
composer require mix/redis-subscriber

订阅频道

  • 连接、订阅失败会抛出异常
$sub = new \Mix\Redis\Subscriber\Subscriber('127.0.0.1', 6379, '', 5); // 连接失败将抛出异常
$sub->subscribe('foo', 'bar'); // 订阅失败将抛出异常

$chan = $sub->channel();
while (true) {
    $data = $chan->pop();
    if (empty($data)) { // 手动close与redis异常断开都会导致返回false
        if (!$sub->closed) {
            // redis异常断开处理
            var_dump('Redis connection is disconnected abnormally');
        }
        break;
    }
    var_dump($data);
}

接收到订阅消息:

object(Mix\Redis\Subscriber\Message)#8 (2) {
  ["channel"]=>
  string(2) "foo"
  ["payload"]=>
  string(4) "test"
}

全部方法

方法 描述
subscribe(string ...$channels) : void 增加订阅
unsubscribe(string ...$channels) : void 取消订阅
channel() : Swoole\Coroutine\Channel 获取消息通道
close() : void 关闭订阅

License

Apache License Version 2.0, http://www.apache.org/licenses/

redis-subscriber's People

Contributors

huangdijia avatar onanying avatar pandaliu-1111 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

redis-subscriber's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.