【问题标题】:Rails and Websockets: need some direction on a featureRails 和 Websockets:需要某个特性的方向
【发布时间】:2013-10-03 22:11:14
【问题描述】:

在我的 rails 应用程序中,User 有一个个人资料,该个人资料可以公开访问。我一直在考虑使用 Pusher gem (https://github.com/pusher/pusher-gem) 在我的应用程序中使用即插即用的 websocket。

基本上我想要这样,如果用户正在查看公共个人资料并且该个人资料的所有者碰巧更新了该个人资料,那么前端会使用新信息进行更新,并且用户会在前端收到通知-更新结束。

任何关于从哪里开始的帮助都会很棒!

【问题讨论】:

    标签: ruby-on-rails websocket pusher


    【解决方案1】:

    每个公开个人资料都有一个唯一的频道名称,例如<user_name>-profile.

    每当用户user_name 的个人资料发生更新时,都会触发用户频道上的事件,传递更新的数据。

    data = update_profile()
    Pusher.trigger( '<user_name>-profile', 'profile-updated', {:profile => data} )
    

    在运行浏览器的个人资料页面上有只在相关频道上监听更新的代码:

    var pusher = new Pusher( APP_KEY );
    var channel = pusher.subscribe( '<user_name>-profile' );
    channel.bind( 'profile-updated', function( update ) {
      // Update UI to show new profile information
      // Show something to indicate that an update has occurred
    } );
    

    这里的一个问题是,即使没有人查看公开个人资料,您也会触发事件。如果您想解决这个问题,您需要使用 WebHooks 并跟踪配置文件频道是否为 occupied,并且仅在是时才触发事件。

    【讨论】:

    • 非常感谢您的提示!我将进一步调查!
    猜你喜欢
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    相关资源
    最近更新 更多