ack/notification-bundle

Symfony 实时通知系统

安装量: 8,893

依赖者: 0

建议者: 0

安全: 0

星标: 25

关注者: 1

分支: 4

公开问题: 1

语言:JavaScript

类型:symfony-bundle

1.0.0 2016-10-15 11:23 UTC

This package is not auto-updated.

Last update: 2024-09-29 23:00:39 UTC


README

介绍

此包提供了使用 Redis 和 Node.js 的通知系统预定义架构

一切均基于 Redis 的 Pub/Sub(发布/订阅)系统,以下是一个简单的场景图

Alt text

每个消息包含一个由 twig 渲染的内容和一个用户 ID 数组。

安装

使用 composer

php composer.phar require ack/notification-bundle

在您的 app/AppKernel.php 文件中注册该包

$bundles = array(
    ...
    new Ack\NotificationBundle\AckNotificationBundle(),
    ...
);

如果您已经在应用程序上运行了一个节点服务器,您可以在 example_server.js 中找到一个实现示例

否则,在资产安装后,您可以进入 /web/bundles/acknotification/nodejs

npm install

node server.js

使用

从控制器或您有权访问 'ack.notifier' 服务的地方

$this->get('ack.notifier')->notify(
    ':notification:test.html.twig', // Any twig file
    array(1, 2, 3), // Array of the users id that need to be notified, use '*' if you want to notify everyone (anonymous users included)
    array() // Optional parameters according the your twig view
);

不要忘记加载 socket.io.js 并连接到服务器。

<script src="http://your.domain:1337/socket.io/socket.io.js"></script>

<script>
    if (typeof io !== 'undefined') {
        var socket = io.connect('http://your.domain:1337');
    }
</script>

一旦您的前端发出 'loaded' 事件,Node.js 将捕获它并将您的用户存储在 Redis 哈希中。这样我们就在某处有一个在线用户的列表,每个哈希包含 socketId。

<script>
    socket.emit('loaded', {
        id : '{{ app.user is not null ? app.user.id : "anon." }}'
    });
</script>

一旦 Node.js 接收到通知,它会对每个用户 ID 发出 'notification' 事件,您可以在前端执行此类脚本以通知用户。

socket.on('notification', function (notification) {
    // get <div> "notifications" and append notification
});

此包依赖于 snc redis,因此不要忘记在您的 config.yml 中添加它。我建议使用 redis 来满足您的其他需求,例如会话存储、缓存、日志等,请参阅:SncRedisBundle

snc_redis:
    clients:
        default:
            type: predis
            alias: default
            dsn: redis://
            logging: %kernel.debug%