官方套件 · 框架

不用 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 完全相同。 免費建立帳號 →