Web Chat System - Most efficent way to achieve Ajax Long Polling or to
surrender to Node.js?
I'm building a traditional chat system where there are rooms and in them
users chatting with each other. I'm using MySql and created three simple
tables of 'Users', 'Rooms' and 'UsersAndRooms' (a table to connect between
rooms and the users in them) The room table has a message field that is
updated each time a user sends a message in the room (concat of current
value + user's message)
My problem is I'm not so sure about how to notify a user about changes in
the rooms he's in, such as other users joining / leaving a room, or that
someone sent a message. I've learned of 2 ways in which this can be
achieved 'Ajax Long Polling' or to use Node.js.
There are 2 ways I can think of achieving this in 'Ajax Long Polling':
Upon Ajax request read from the db until a change occurs and only then
notify the user which kinda sucks cuz' it will mean a lot of IO calls..
Upon Ajax requests that makes me write to the DB (such as SendMessage)
I'll have a dictionary of room to timestamp as a server variable and turn
the timestamp of a room to current time, then upon an ajax request to read
I'll wait for this timeout to be between NOW and 5 seconds before NOW The
idea here is upon someone making a write to the DB for a certain room, I
want to open a window of 5 seconds in which I want to tell all ajax
requests who are waiting that they can continue. I'm not so sure if
something like this is even possible in php I do remember however having
this ability in old ASP (using the Application keyword)
I want to know if option 2 makes sense, if there's a better way and if
maybe using Node.js is just ultimately the best solution for what I need.
I prefer not to have to go for Node.js because it has less servers
supporting it and it'll take some time to learn how to use it and how to
use it with Ajax.
No comments:
Post a Comment