如何在 Rails 3 中查看用户的实时活动? [英] How do I see real-time activity of my users in Rails 3?

查看:25
本文介绍了如何在 Rails 3 中查看用户的实时活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是让我的管理员用户能够实时(通过一些 AJAX/jQuery 功能)看到我的用户在做什么.

What I would like to do is have my admin user be able to see - in real time (via some AJAX/jQuery niceness) - what my user's are doing.

我该怎么做?

我认为它与会话活动有关 - 我已经开始将会话保存到数据库,而不是 cookie.

I assume it has something to do with session activity - and I have started saving the session to the db, rather than the cookie.

但一般来说,我如何获取该信息并实时解析它?

But generally speaking, how do I take that info and parse it in real time ?

我查看了会话表,除了 id(id 和 session_id)之外,我还看到了一个数据"字段.该数据字段存储一个散列 - 我无法理解(看起来像一个 md5 散列).

I looked at my session table and aside from the ids (id and session_id), I see a 'data' field. That data field stores a hash - which I can't make any sense of (looks like an md5 hash).

我如何使用它来查看用户 A 刚刚点击了链接 B,然后用户 B 立即点击了链接 A,等等?

How would I use that to see that User A just clicked on Link B, and right after that User B clicked on link A, etc. ?

除了rackamole之外还有什么宝石可以帮助我吗?

Is there a gem - aside from rackamole - that might be able to help me?

推荐答案

会话数据仅包含存储在用户的 session[]-hash 中的值.它不存储调用了哪个动作/控制器,因此您不知道点击了哪个链接".

The session data only contains the values stored in the session[]-hash from the user. It doesn't store which action/controller was called, so you don't know which "link was clicked".

除了rackamole,恕我直言,您还有两种选择.

Besides rackamole you have two options IMHO.

  • 在您的 ApplicationController 中使用 before_filter 来存储您感兴趣的相关信息.(控制器名称、动作或 URI、附加参数和 id例如登录用户).

  • Use a before_filter in your ApplicationController to store the relevant info you are interested in. (Name of controller, action or URI, additional parameters and id of the logged in user for example).

在每个页面的底部使用 AJAX 调用,将您感兴趣的信息(URI、登录用户的 ID 等)回发到您的服务器.这允许来自服务器的更快响应时间,因为信息是在页面已经交付之后存储的.另外,您不必使用 Rails 请求来存储它.AJAX 请求也可以调用一个简单的 PHP 脚本,将数据写入磁盘.这要快得多.

Use an AJAX-call at the bottom of each page which posts back the info you are interested in (URI, id of logged in user, etc.) to your server. This allows faster response times from the server, as the info is stored after the page has already been delivered. Plus, you don't have to use a Rails-request to store it. The AJAX-request could also be calling a simple PHP-script writing the data to disk. This is much faster.

将此数据/信息存储在数据库或日志文件中.数据库将为您提供更大的灵活性,例如显示来自一个用户的所有操作或一个页面的所有访问者等.日志文件解决方案将为您提供更好的性能.

Store this data/info either in the database or in a logfile. The database will give your more flexibility like showing all actions from one user, or all visitors for one page, etc. The logfile solution will give you better performance.

至于实时提取收集到的数据,您必须构建自己的解决方案.要优雅地执行此操作(无需每秒查询一次服务器以查看新数据是否已到达),您将需要另一个服务器进程.搜索 AJAX 推送了解更多信息.

As for pulling out your collected data in realtime, you have to build your own solution. To do this elegantly (without querying your server once a second to look if new data has arrived) you'll need another server process. Search for AJAX Push for more info.

根据您的应用程序,我会问自己是否真的需要实时通知(因为设置起来很麻烦).

Depending on your application I'd ask myself if realtime notifications for this are really necessary (because of all the hassles of setting this up).

要监控您网站上的活动,拥有一个列出最新操作的页面并手动刷新(或每十秒自动刷新一次)就足够了.

To monitor the activity on your site, it should be enough to have a page listing the latest actions and manually refresh it (or refresh it automatically every ten seconds).

这篇关于如何在 Rails 3 中查看用户的实时活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆