【问题标题】:Rails 4.2 What's currently the best way to incorporate instant messaging in an app?Rails 4.2 目前在应用程序中加入即时消息的最佳方式是什么?
【发布时间】:2015-09-09 20:14:33
【问题描述】:

我一直在读到 Rails 5(有人知道什么时候发布吗?)将合并对 websockets 的支持,这将使即时消息更容易合并。但我现在正试图为一个相当完善的应用程序设置一些东西。如果一切顺利,我很快就会拥有相当多的用户,所以它也需要扩展。

我看过 Ryan Bates 的私人酒吧,它有点老了,Heroku's websocket example(我部署在 Heroku 上)、Websocket-rails、actioncable,也许还有其他一些。大多数看起来都很复杂,所以我想知道我最好的选择是什么,或者 Rails 5 是否会很快推出,我应该等待吗?

谢谢。

【问题讨论】:

    标签: ruby-on-rails-4 websocket instant-messaging ruby-on-rails-5 actioncable


    【解决方案1】:

    我偏向于 Plezi,它是为扩展(使用 Redis)和 can be used to easily add websocket support to your existing web-app 而构建的...但再说一遍,我可能对此并不客观。

    有两种方法可以在 Rails 中运行 Plezi - 合并应用程序/服务器(使用 Iodine HTTP/Websocket server)或使用 Redis 同步两个应用程序。

    这两种方式都很容易设置。

    要在您的 Rails 应用程序中使用 Plezi,请将 plezi 添加到您的 Gemfile 并从您的 Gemfile 中删除任何对“thin”或“puma”或任何其他服务器的引用 - 这应该允许 Iodine 自动接管.比将 Plezi.app 作为中间件放置在您的应用中。

    您可以通过要求它的文件来包含预制的 Plezi 应用程序,或者 - 甚至更简单 - 您可以将代码写入 Rails 文件之一(可能使用“initializers”、“helpers”或“models”文件夹)。

    尝试为聊天室服务器添加以下代码:

    require 'plezi'
    
    # do you need automated redis support?
    # require 'redis'
    # ENV['PL_REDIS_URL'] = "redis://user:password@localhost:6379"
    
    class BroadcastCtrl
        def index
            # we can use the websocket echo page to test our server, 
            # just remember to update the server address in the form.
            redirect_to 'http://www.websocket.org/echo.html'
        end
        def on_message data
            # try replacing the following two lines are with:
            # self.class.broadcast :_send_message, data
            broadcast :_send_message, data
            response << "sent."
        end
        def _send_message data
            response << data
        end
    end
    
    route '/broadcast', BroadcastCtrl
    

    这允许我们将一些 Rails 魔法注入 Plezi 并将一些 Plezi 魔法注入 Rails...例如,很容易保存用户的 websocket UUID 并向他们发送更新:

    require 'plezi'
    
    # do you need automated redis support?
    # require 'redis'
    # ENV['PL_REDIS_URL'] = "redis://user:password@localhost:6379"
    
    class UserNotifications
        def on_open
            get_current_user.websocket_uuid = uuid
            get_current_user.save
        end
        def on_close
            # wrap all of the following in a transaction, or scaling might
            # create race conditions and corrupt UUID data
            return unless UsersController.get_current_user.websocket_uuid == uuid
            get_current_user.websocket_uuid = nil 
            get_current_user.save
        end
        def on_message data
             # get data from user and use it somehow
        end
        protected
        def get_current_user
             # # use your authentication system here, maybe:
             # @user ||= UserController.auth_user(cookies[:my_session_id])
        end
        def send_message data
            response << data
        end
    end
    
    route '/', UserNotifications
    
    # and in your UserController 
    
    def UserController < ApplicationController
        def update
            # your logic and than send notification:
            data = {}
            UserNotifications.unicast @user.websocket_uuid, :send_message, data.to_json
        end
    end
    

    【讨论】:

    • 谢谢。我正在尝试在我的 Rails 应用程序中使用它,但我对如何使用它有点困惑。有我可以查看的示例 Rails 应用程序吗?我也遇到了这个错误:“试图更新执行权限。这取决于系统,可能已经失败。”你知道那是怎么回事吗?它不会让我从 plezi install 运行默认的 plezi 应用程序。
    • 消息"tried to update execution permissions. this is system dependent and might have failed" 不是错误。 Plezi 总是这样写,因为执行权限更新是基于 MacOSX 的,应该可以在类 unix 系统上运行,但不能在 windows 上运行。这只是一个 tweek,允许您从操作系统的 GUI 以及终端 UI 运行应用程序......其余的我将在编辑答案时尝试编写。
    • 感谢您的支持。我发现仅仅安装 gem 会显着降低 javascript 的速度,至少在开发模式下是这样。这正常吗?我仍然很难将我的头包裹在所有东西都去哪里并融合在一起。我想我可能会暂时推迟包括 websockets。
    • 这不太正常。虽然,0.11.0 版应该很快就会推出,GRHttp 的新版本,所以这可能会解决这个问题......不过,我还是非常惊讶。我唯一能想到的是 JavaScript 等待连接关闭,而服务器保持连接处于活动状态......您可以尝试添加 connection: close 标头,看看是否有帮助。
    • @baron816 - 版本 0.11.0 已经发布,我想知道问题是否仍然存在...您能否检查一下,因为问题没有出现在我的系统上?
    【解决方案2】:

    Rails 5 已发布。我建议您升级并使用 actioncable。

    从长远来看,它应该是最佳选择,因为它将成为 Rails 核心的一部分,并且由 Basecamp 开发、使用和维护。他们将投入足够的精力来确保它稳定、可扩展并被社区接受。

    【讨论】:

    • 感谢您的建议。动作电缆是我最终要使用的。我花了很多时间尝试将其整合到我的应用程序中,但它似乎确实依赖 Rails 5 来完成除了最基本的实现之外的任何事情。而且它绝对没有完成。所以我决定推迟到一切准备就绪。
    • 这仍然是真的吗?你可以在 rails 4 中使用 actioncable gem 吗?
    • 我无法将 actioncable 与 rails4 一起使用,它依赖于依赖于 rails 5 的 actionpack5
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 2010-12-05
    相关资源
    最近更新 更多