M
M
Max Kuznetsov2011-05-10 01:41:15
PHP
Max Kuznetsov, 2011-05-10 01:41:15

Chat backend design?

There is a task to make something like a chat. For example, there are a hundred participants who have the ability to connect to the server via sockets (not the web and comet, but sockets). When one of them sends a message to the server, it is forwarded to the others. Faced with the fact that I don’t know how to properly design the server side (preferably in php). Do I need separate threads to listen and write to a socket? Will this whole system work on one port? Help me to understand! Explain at least schematically.

ps^ Obviously, my main problem is a misunderstanding of the mechanisms for working with sockets.

Answer the question

In order to leave comments, you need to log in

7 answer(s)
C
CKOPOBAPKuH, 2011-05-11
@pluseg

judging by your answers, the problem now is not in the language, but in the architecture and understanding of how it will work.
with a small load (and this is exactly the situation you have, at least now, right?), the difference between languages ​​​​is only in convenience.
The most convenient way to do this, in my opinion, is in node.js.
algorithm:
if a client has connected, store it in an array (hash, set, or whatever it's called, it doesn't matter). if the client disconnects, remove from the array. if the client sent something, then we receive the message, run through the array and send this message to all clients that are in the array. all.
after that we add exception and error handling.
in php in the same way, only handling sockets is a little more complicated.

S
shsmad, 2011-05-10
@shsmad

you invented IRC :)

A
Alex Bunin, 2011-05-10
@azxc

Business, of course, master's, but I would not do through sockets. And would make through normal Web service. However, Apache (or another Web server) would be better off supporting a large number of concurrent clients and such.
And in the scripting language, a fairly minimal processing of requests and delivery of messages to the rest who are currently logged into the system will be done.

R
rPman, 2011-05-10
@rPman

PHP is not very suitable (more precisely, it does not imply such a use, but formally nothing prevents this) for use as a constantly running service, instead of the mode of frequent and short starts
. Accordingly, asynchronous sockets can be used (see socket_set_nonblock and socket_set_option, the first example in Google) in php and run the server part as a single process (with all the ensuing problems due to potential bugs in the code, or rather, all connections will be interrupted if this application crashes), or run per process for each connection, with ordinary sockets, but then good welcome to the world of semaphore and shared memory (of course, you can do without them, using a typical LAMP approach, when a database with transactions is responsible for synchronization, but the performance will be terrible and you will be laughed at for shitty code).
ps Strongly did not dig, perhaps there are a bunch of ready-made frameworks or even PHP extensions that add event functionality.

A
Alexander, 2011-05-10
@Alexx_ps

Get a ready solution. Alternatively, open source, if you want to dig deeper yourself, at the same time you will learn how the backend works there.

S
SolarSoul, 2011-05-10
@SolarSoul

No bike needed)
Jabber/XMPP server on Erlang

V
Vitaly Peretyatko, 2011-05-11
@viperet

pushmodule.slact.net/ - just a great thing for all sorts of chats (this is a module for Nginx that implements http-push). Connections, queues, etc. are handled by nginx - believe me, it can do it well))))
And for the application - a simple interface that allows you to subscribe to channels or send messages to them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question