JDare / ClankBundle

A Symfony2 Bundle for use with Ratchet WebSocket Server
MIT License
131 stars 31 forks source link

Send Publish call from within a Controller #7

Open teagup opened 11 years ago

teagup commented 11 years ago

Hi, I'm working on a project requiring a notification to be sent every time a user takes an action from within a controller. I know it's possible to send publish requests from frontend javascript using a session.publish(); call, but I'm hoping to achieve the same kind of functionality from a controller. (ie user takes an action -> Controller gathers a list of users needing to be notified and does some other functionality -> Controller sends a publish call to Notification Topic for each user -> Notification Topic sends an appropriate message to each user.)

Thanks for the great bundle, Patrick

JDare commented 11 years ago

Hey Patrick,

Talking to the daemon from the controller is a little tricky as they are running in separate processes. Off the top of my head theres a few approaches to the problem, either using a work queue or just opening a web socket from the request.

The first and maybe more "proper" way to do it, is using process to process communication (something like zeromq or a work queue such as beanstalk). Although this would involve writing more code on the server side to handle these requests as well.

The second one can be achieved by opening a websocket from the request directly to the server daemon, include a special "admin" payload that can notify the daemon that a controller request has been made and it must notify the other connections. https://github.com/Devristo/phpws apparently contains a websocket client, so maybe give that a go.

Thats all I can think of at the moment, but its definitely not a definitive list as i'm sure theres plenty of other methods available.

teagup commented 11 years ago

Thanks for the speedy reply! I think I'll try out using zeromq or beanstalk, since we're going to be deploying on a cluster of servers. I'll let you know what I come up with as far as a generic solution.

jponnoua commented 9 years ago

Hello,

I'd like to publish data from the server to the clients, through the subscribed topics. As you mentionned above, there is an issue of context. This is not my case: I'd like to publish a message when a disconnection is performed (onClientDisconnect method triggered from the server).

Do you have any idea ?

Regards,

Julien.

jjsaunier commented 9 years ago

I'm not sure to understand, you want web socket client on php server side ?

jponnoua commented 9 years ago

Yes, in the EventListener, into the method public function onClientDisconnect(ClientEvent $event), I'd like to publish a message to my friend list to let them know the user will be disconnected.

jjsaunier commented 9 years ago

You must achieve this with unSubscribe method from topic instead of onClientDisconnect because onClientClientDisconnect is out of scope of WampServer. I mean you don't have topic id (channel) and subscribers, you are in IoServer (more details here : http://socketo.me/docs/design).

The only things you have to do, is to detect from client when the user trigger unload event and then call session unsubscribe.

jponnoua commented 9 years ago

Ok, thank you !

jjsaunier commented 9 years ago

If you are interested I have fork ClankBundle to my own organisation in order to maintain it actively, because currently this bundle is not maintained.

You can see it here : https://github.com/GeniusesOfSymfony/WebSocketBundle