2016-10-26 11 views
3

私はRails 5 APIのみを作成しており、リアルタイムのWeb通知を作成する必要があります。Rails 5 API +リアルタイム通知

ActionCableを使用すると可能ですか?誰かがアクションケーブルやその他のソリューションの例を持っていますか?

ありがとうございます。

答えて

4

これは、あなたが右のストリームにブロードキャストするときに、クライアント側のWeb通知をトリガすることを可能にするウェブ通知チャネルである:

は、サーバー側のWeb通知チャンネルを作成します。

# app/channels/web_notifications_channel.rb 
class WebNotificationsChannel < ApplicationCable::Channel 
    def subscribed 
    stream_for current_user 
    end 
end 

の作成しますクライアント側Web通知チャネルサブスクリプション:

# app/assets/javascripts/cable/subscriptions/web_notifications.coffee 
# Client-side which assumes you've already requested 
# the right to send web notifications. 
App.cable.subscriptions.create "WebNotificationsChannel", 
    received: (data) -> 
    new Notification data["title"], body: data["body"] 

アプリケーションの別の場所からのWeb通知チャネルインスタンスへのコンテンツのブロードキャストication:

# Somewhere in your app this is called, perhaps from a NewCommentJob 
WebNotificationsChannel.broadcast_to(
    current_user, 
    title: 'New things!', 
    body: 'All the news fit to print' 
) 

WebNotificationsChannel.broadcast_toコールはユーザーごとに個別の放送名の下に、現在のサブスクリプション・アダプターののpubsubキューにメッセージを配置します。 IDが1のユーザーの場合、ブロードキャスト名はweb_notifications:1になります。

関連する問題