1

I want to implement a PHP chat with multiple Rooms, however I don't want each browser polling the server, instead I prefer the server send the updates to all users in each room. Ideally I would have just a PHP instance running for each room (plus of course the AJAX requests sent by users for updating the DB, I know server side events are not widely supported):

  • users POST messages using a POST AJAX request
  • when the PHP script of a Room read the DB and find a new message, it will sent the update to ALL the users connected to that room, this way it will be more responsive and would put less pressure on DB communication

So basically If there are N users and K rooms I want to reduce the overhead from

N database/php poll requests every while

to

K database/php poll requests every while
CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
  • 1
    i think this could be of help: https://github.com/walkor/phpsocket.io it even has a chat example in the readme – valepu Jul 08 '16 at 10:11
  • thanks I'll consider that, however I was searching for something simpler than a socket to code because of time constraints, EDIT: ehy that library and Workerman have a lot of effort into, they seems far more scalable and complete than any other framework, however that would require sometime to get the graps out of those – CoffeDeveloper Jul 08 '16 at 10:12
  • I don't think there's an alternative if you want to let your server send requests to the clients and use only PHP, you will need websockets. The alternative is "long polling" http://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet – valepu Jul 08 '16 at 10:17

2 Answers2

2

You might better use web sockets for this purpose. If you want to use php, there are few libraries for that:

1) Ratchet

2) ReactPHP

3) d-Node

and others. I used Ratchet and React. They work fine, as for me

D.Samchuk
  • 1,219
  • 9
  • 9
  • both 1) and 2) assumes I can access the php program to launch the script from SSH on the remote machine. That's why I was asking for php events instead of using a pre-made library.. Thanks anyway:) – CoffeDeveloper Jul 08 '16 at 10:39
1

Yes, but it will require writing your own web server: i.e. a socket server in PHP to receive http requests from clients. You then just keep one array of sockets per chat room, and when you get a message you want to broadcast to all listeners, you create and send an SSE message to each client, something like:

data: {room:12,msg:"Hello World"}\n\n

(I think by registering the socket into multiple arrays that you can even use a single SSE connection to listen to messages from multiple chat rooms. So, you could even have a single PHP process running all chat rooms.)

However, if using, say, Apache+PHP, then what you want is not possible. Each SSE connection will get a dedicated PHP process. (If this is your only choice and polling the DB is really expensive, you could have a single process poll the DB, then push messages to an in-memory localhost DB, and have each PHP process poll that in-memory DB.)

Darren Cook
  • 27,837
  • 13
  • 117
  • 217