开发者

See and talk to your currently online users on your site in a Ruby on Rails 3 application

开发者 https://www.devze.com 2023-03-17 10:42 出处:网络
I have a Ruby on Rails 3 application, and I want开发者_如何学运维 to be able to see a list of who is currently online. For example user1, IP address, and country. I then want to be able to open a chat

I have a Ruby on Rails 3 application, and I want开发者_如何学运维 to be able to see a list of who is currently online. For example user1, IP address, and country. I then want to be able to open a chat / push messages to this user until they leave my site.

How can I accurately monitor who is currently on the site and instantly remove the user from the list when they leave?

I then can talk to them via faye pub/sub.


How can I accurately monitor who is currently on the site and instantly remove from list when they leave?

Well using HTTP you can not do this "instantly" in a browser. Almost all solutions I see use a heartbeat technique. Every X seconds, a request is sent from the browser (using Ajax), that tells if the user is online. If you haven't heard from the user in x heartbeats, you regard the user as disconnected - even Facebook uses this, it seems. I will recommend you to drop your requirement for instant, unless it's really important.

Another approach is to implement Flash or Silverlight, to make a socket connection to the server. But the demand on the server is high, and if many people is on your site, you will run into trouble with ports and so on.


I think this is not so much related with Ruby on Rails... but this is very hard to implement in HTTP with a scripting language only. The server does not know whether a user has closed the browser or not. The server just sends the requested page data to the user and closes the connection.

You would rather have to integrate Ajax or Flash to make things easier. I have seen some people developing chat programs with Flash, and it seems to work much better than any other Ajax-implemented chat programs.

Chat is very unfavorable in a web browsing context, since the page will be reloaded as the user clicks a link. If you are thinking about building an application that only supports a chat feature, you probably want to look something other than Ruby on Rails. For example, Node.js will be a good one.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号