views:

54

answers:

2

Hello,

I am trying to create an inline chat. The problem arrives when two windows of my site are open. Because then my script sends the new message to the the window which requests it for (for the time being i am using simple polling, will move to comet later).

Is there any good technique that I haven't thought of, so that I can send a message to both the browser windows? Currently I am using a read flag, so as soon as the message gets delivered to one window, it is marked as read. Thus never getting delivered to the other window. How do I solve this problem?

Thank you for your time.

+1  A: 

One thing I can think of is to use some sort of timestamp on the messages, instead of the "read" flag.

This way, when the page request for the new messages (and I'm assuming uyou're using some sort of timing to pool the server for new messages) is sends the last timestamp it got, then the server sends back all the messages after that point.

Paulo Santos
A: 

Each window needs to be a separate "subscriber", and the server needs to send the message to each subscriber. So instead of one "read" flag you need a "read" flag for each subscriber.

Unfortunately this means making a subscriber manager with a queue for each subscriber and a way of knowing when a subscriber disappears -- perhaps when the subscriber stops polling for one minute.

Look up the "Observer" pattern (also known as "Publish-Subscribe") in the Gang of Four book ("Design Patterns: Elements of Reusable Object-Oriented Software" by Gamma, Helm, Johnson and Vlissides", ISBN 0-201-633610-2) or online.

Mark Lutton
this will be a real pain to manage.
Alec Smart