Fork messages?

37 views
Skip to first unread message

omarvil...@gmail.com

unread,
May 21, 2014, 9:59:31 PM5/21/14
to ratch...@googlegroups.com
I am implementing a chat server using Ratchet. I've implemented a dispatcher that accepts service(params) type of messages encoded in json, similar to a RESTFUL call, but with websockets. So every request is parsed in the onMessage method and dispatched to its proper controller. The problem is that every message is processed by Ratchet in a serial fashion, one at a time. For example, if I add a sleep(n) inside and someone else tries to open a connection, it won't be accepted until the sleep() ends. This totally kills it for me, so I thought about forking every request, so that it runs on its own process. Now, the problem with that is that php's pcntl_fork seems to make an exact copy of everything, including the connections collection, so when I want to send a response to the client, there's no way for me to get the proper connection handler, I seem to get a copy of it, because nothing is sent to the client and no error is given. 
So I thought about forking every connection instead of every request, but I'm completely lost there.

What do you guys suggest? I'm quite new on this type of php programming. All help will be much appreciated.

cboden

unread,
May 24, 2014, 10:36:20 AM5/24/14
to ratch...@googlegroups.com, omarvil...@gmail.com
Ratchet is asynchronous, not concurrent. All script execution blocks so you don't want to do anything that isn't executed immediately (sleep is bad). Forking is not the answer; it is very resource intensive and slow. 

Check out the React EventLoop. It is what powers Ratchet and has timeout functions that will execute after a specified amount of time without blocking the application. 

Omar Villanueva

unread,
May 24, 2014, 7:08:47 PM5/24/14
to cboden, ratch...@googlegroups.com

Thanks a lot for the answers.

I need an almost real time solution for this chat server, so timed events are not the answer either. BTW, I was able to implement what I wanted by ditching React altogether, using components of Ratchet and forking.
But, is forking's performance a complete showstopper for this according to you? I did not do any stress test yet.

cboden

unread,
May 31, 2014, 12:44:28 PM5/31/14
to ratch...@googlegroups.com, cbo...@gmail.com, omarvil...@gmail.com
Each fork creates another running process by copying all resources from the existing process. So if you fork for every message you're creating a cloned process for all resources, not just the message. 

Forking is very useful when used in the right circumstances but I don't think forking a process on every message or even connection will be sustainable. 

Out of curiosity what are you using to do I/O in place of React? 

andre...@gmail.com

unread,
Feb 9, 2015, 3:59:17 AM2/9/15
to ratch...@googlegroups.com, cbo...@gmail.com, omarvil...@gmail.com
Hi,

Can you provide a short example on how to add a timeout function that will run "in background"?

Best regards,
Andreas
Reply all
Reply to author
Forward
0 new messages