views:

141

answers:

2

Hello,

I'm trying to use FlashMessenger to notify another user of an event. Does anyone know if this is possible?

Something like

$flashMessenger->addMessage( array('status'=> '', 'message'=> 'Hello!'), $user);
+1  A: 

Hi,

Quoting the manual page of the FlashMessenger :

The FlashMessenger helper allows you to pass messages that the user may need to see on the next request. To accomplish this, FlashMessenger uses Zend_Session_Namespace to store messages for future or next request retrieval.

So, the messages are stored in session -- and a session is attached to / corresponds to a user ; the current user, actually.

The session is not meant to store data that is shared between different users -- so I would say that this component cannot be used to notify other users of an event ; not natively, at least.


A possible solution would be :

  • when you detect there is a message that must go to other users, store it in database (some table with a foreign key pointing to the destination user, if the destination user is a connected-user ; some table storing the message, if it can be seen by anyone).
  • on each page, you check in that DB table if the is a message that has to be displayed
  • if yes, you put it in the FlashMessenger, which will display it on next page load from the current user.

A bit tricky, and not as easy as you'd hope, I admit...

Another idea, instead of using a database, would be to use some Caching engine (like APC, memcached, ... see Zend_Cache, to avoid hitting the DB.

Have fun !

Pascal MARTIN
Great stuff Pascal. I was going to use a database, but I think you are right with using z_cache.
Sledge
Thanks :-) Just a note : using cache means your message could never arrive to destination : cache cane expire, and if there is no place left in cache, the caching engine will most likely remove some old/unused entries -- but, generally, this will be OK, if your messages are not critical.
Pascal MARTIN
A: 

Other option would be to implemment your own session handling and store it in DB (along with username). You can then access it and alter it in any way. We've implemented this when we needed to get past some crazy restrictions (one day limited session lifetime) of hosted enviroment's session setup. It works really good and offers much more possibilities over the default implementation (like sign out all users if some specific user signs in - superadmin for example or signout user if his password is changed in admin section, etc.).

But I guess this is a bit overkill for your purposes. And Pascal's way would be good enough.

Tomáš Fejfar