Can I run a code on a random (not current) user browser?

Hello folks.
I made a messenger on my website, so users can send messages to each other. When a massage is sent, a new item with ‘From’ user ID and ‘To’ user ID is created in my Messages collection. Is there a way to notify ‘To’ user, if he/she is logged in, once a message has been sent? Like showing a popup or any other alert in that user’s browser? I know ‘current user’ API, but not sure how to handle other users with a code (or if it’s possible at all). Thanks for any piece of advice.

You can show an alert on your site tab only.

I also have read your post, but did not understand this sentence here…

Is there a way to notify ‘To’ user, if he/she is logged in, once a message has been sent?

  1. Who exactly is logged-in?
  2. Notify who?

You want to notify the recipient, that a message is available for him?

Of course the real-time-API will be the right answer here, but are there also workarounds existing?


  1. User-A creates a messsage and sends it to the → Messages-Collection
  2. Of course the message is saved with a user-ID → sending-user.
  3. But surely you also will have generated a DB-Field called recipientID, or similar, to make sure the right person will get the message.
  4. Let’s call the two ID’s → senderID / receipt ID

Soo, User-A sends the message including the two-IDs and the data is saved inside your Messages-Collection.

How to notify now the → recipient?

You can generate on the MASTER-PAGE an ----> INTERVAL-FUNCTION , which will check for example every x-seconds/minutes for changements inside your Messages-DB .

If a changement is registered → you grab the last changed data (regardind the most actual changing-date).

Now you are able to find the last UPDATED-DATA → getting its both IDS and continuing what ever you want to do with those two IDs .

But what’s with the ALERT-MESSAGE ?

What about a → Poping-Up-Lightbox → which will appear emmedialtely, when a changement inside DB has been found?

This description, shows you just a workaround, of how to set-up your wished functionality the MANUAL-way.

But perhaps you better should take a look onto J.D.'s provided → Real-Time-API!

@russian-dima Thanks for your answer. You have guessed it correct, I would like to notify the recipient, who is logged in when a message is sent.

Actually I tried ‘checking the DB every x-seconds’ method, and it worked. I just thought it could be too abusive requests to DB, and there might be a more rational method, like an after-insert hook that only fires a code when a new message is created in a collection. I will check Real-Time API as well, thanks again!

I’ll check the link, thank you J.D.!