User's presence in the chat (chat updated by a simple poll)

We implemented a simple chat feature in Rails using simple Ajax updates. Now in each chat room a message belongs to a specific user. We want to show a list of users (something like user presence). Please suggest ways. We do not use Jabber, XMPP, etc.

Chatroom Model:

class ChatRoom < ActiveRecord::Base validates_presence_of :title has_many :messages,:foreign_key=> "chat_room_id" has_many :stories,:foreign_key=>"chat_room_id" has_many :topics,:foreign_key=>"chat_room_id" end 

Messages are chats sent to each user.

Message Model:

 class Message < ActiveRecord::Base belongs_to :user end 

User Model:

 class User < ActiveRecord::Base acts_as_authentic :crypto_provider => Authlogic::CryptoProviders::BCrypt validates_presence_of :nick validates_uniqueness_of :nick has_many :questions end 

Please suggest ways

+4
source share
1 answer

To keep track of which users are in which room, you can establish a HABTM connection between the ChatRoom and User models. And you can add the last_poll_datetime column to the User model to track the last time the user polled messages (more on this part in a minute).

To list all users in a given room, use the HABTM join table, ChatRooms_Users. You will insert / remove from this table whenever a user joins or leaves the room.

If you want users who close their browsers to close their browsers instead of clicking β€œleave the room”, set up an outsole task to run every minute, which searches for users with last_poll_datetime older than one minute and removes their rows from the ChatRooms_Users connection table.

+4
source

All Articles