官方包 · 框架

无需 Mercure hub 的 Symfony UX Turbo Streams

Doctrine 的 #[Broadcast] 实体、Turbo Streams 和 Symfony Messenger 通过 Hibira 发送更新,浏览器通过 WebSocket 接收。无需部署 Mercure hub。

  • #[Broadcast]、turbo_stream_listen() 和 turbo_stream_from() 的用法与 Mercure 相同,只是 transport 名为 hibira。
  • 每条更新都是一条 Messenger 消息,可以交给异步 worker 发送,网络慢也不会拖慢请求。
  • 还能为您自己的私有和 presence 频道签名、验证 Webhook,并签发推送通知令牌。支持 Symfony 6.4、7 和 8,UX Turbo 2 和 3。

登录后,示例代码会自动填入您的集群主机。 登录

  1. 安装

    composer require hibira-io/symfony-bundle
    
    # .env.local
    HIBIRA_APP_ID=YOUR_APP_ID
    HIBIRA_APP_KEY=YOUR_APP_KEY
    HIBIRA_APP_SECRET=YOUR_APP_SECRET
    
    php bin/console hibira:check
  2. 配置

    use Symfony\UX\Turbo\Attribute\Broadcast;
    
    #[ORM\Entity]
    #[Broadcast]
    class Message
    {
        // ...
    }
  3. 使用

    {# templates/broadcast/Message.stream.html.twig #}
    {% block create %}
    <turbo-stream action="append" target="messages">
      <template>{{ include('message/_message.html.twig', { message: entity }) }}</template>
    </turbo-stream>
    {% endblock %}
    
    {# the page #}
    <ul id="messages" {{ turbo_stream_listen('App\\Entity\\Message') }}></ul>

开始之前

  • 应用密钥只会在创建应用或轮换密钥时显示一次。请只放在服务器上,不要放进浏览器或游戏的构建包。
  • Free 套餐需要把页面加载的每个来源都加入应用的“允许的来源”(开发时是 http://localhost:3000,之后是您的域名)。"*" 需要付费套餐。

请使用控制台中应用的 Key;Secret 只放在你的服务器上。频道、事件与授权端点的用法与 Pusher 完全相同。 免费创建账号 →