0

我一直在阅读Rails 5(任何人都知道什么时候会发布?)将加入对websockets的支持,这将使即时消息更容易并入。但是我正试图为一个相当成熟的应用程序设置一些东西。如果一切顺利,我可能会有很多用户,所以它也需要扩展。Rails 4.2什么是目前最好的方式来在应用程序中加入即时消息?

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

谢谢。

回答

2

我偏向于Plezi,它是为缩放(使用Redis)和can be used to easily add websocket support to your existing web-app而构建的...但是再次,我可能不会客观。

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

两种方式都很容易设置。

为了您的Rails应用程序中使用Plezi,添加pleziGemfile,并转移到“薄”或“美洲狮”,或从Gemfile任何其他服务器的任何引用 - 这应该允许碘自动接管。比将Plezi.app作为中间件放置在您的应用中。你可以通过要求它的文件包括一个预先制作的Plezi应用程序,或者 - 更简单 - 你可以将代码写入Rails文件之一(可能使用'initializers','helpers'或'models'文件夹)。

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

require 'plezi' 

# do you need automated redis support? 
# require 'redis' 
# ENV['PL_REDIS_URL'] = "redis://user:[email protected]: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:[email protected]: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 
+0

谢谢。我试图在我的Rails应用程序中使用它,但是我对如何做到这一点感到困惑。有没有我可以看到的一个示例rails应用程序?我也得到这个错误:“试图更新执行权限,这是依赖于系统并可能失败。”你知道那是关于什么吗?它不会让我从plezi安装运行默认的plezi应用程序。 – baron816

+0

消息''试图更新执行权限,这是依赖于系统的,可能失败了“'不是错误。 Plezi总是写这个,因为执行许可更新基于MacOSX,并且应该在类Unix系统上工作,但不能在Windows上工作。这是一个合理的tweek,它允许你从操作系统的GUI以及终端UI运行应用程序....其余的我将在编辑答案时尝试编写。 – Myst

+0

感谢您的支持。我发现只要安装gem会减慢JavaScript的速度,至少在开发模式下是如此。这是正常的吗?我仍然很难把所有东西都放在一起,并且放在一起。我想我可能会延期包括websockets一段时间。 – baron816

0

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

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

+0

感谢您的建议。行动电缆是我将要使用的。我花了很长时间试图融入到我的应用程序中,但它似乎依赖于Rails 5,除了最基本的实现之外。它绝对没有完成。所以我决定暂缓,直到一切准备就绪。 – baron816

+1

这是真的吗?你可以使用rails 4的actioncable gem吗? –

+0

我还没有能够使用rails4的actioncable,它取决于actionpack5,这取决于轨道5 –

相关问题