4

There is a website which uses socket.io as its 'API'. Essentially, anyone can download the source code of the website and modify it to make their own client with little difficulty. For example, I have such six clients that all run the same custom client side script. The script is hosted on my own domain, not the one with the socket.io API.

I want to be able to keep track of who is connected, and keep a total of certain data. For example if each individual client has 0.5 balance, I want them to be able to know that the total is 3 balance, because of the server.

The most obvious approach I can think of is to do AJAX couple with calls to MySQL to keep track of things, but that seems long-winded.

I could also do CURL but that would be technically challenging.

Is there a simple and straight forward way of simply totaling a balance from six different socket.io clients and sending the information back to them?

  • 2
    I'm afraid that the most straightforward way is to do it in Node.js. There are Socket.IO clients out there for other languages, but they sometimes lag behind and don't always implement the full protocol. Socket.IO uses a bunch of "transports" for its own protocol. Everything from AJAX to Websockets. You only need one transport to communicate, but this isn't a small task. – Brad Aug 05 '13 at 21:03
  • possible duplicate of [Using PHP with Socket.io](http://stackoverflow.com/questions/6398887/using-php-with-socket-io) – SheetJS Aug 05 '13 at 22:42
  • You might want to look into [React](http://reactphp.org/). It's a PHP library for handling websockets. I haven't played with it yet but it looks promising and I hear great things. – stevenwadejr Aug 15 '13 at 03:00

1 Answers1

0

Use an object caching layer like APC or memcached as shared memory, in your PHP backend. That way, you can maintain synchronization between different instances/clients.

Bear in mind that you will have to implement some multiprocessing security as this is not thread safe.

For instance:

/* PHP application 1: */
(($value = apc_fetch("IncrementValue")) != FALSE) ? apc_store("IncrementValue",$value+1) : apc_store("IncrementValue",1);

Another can then access it just like this:

echo apc_fetch("IncrementValue");

It is a fast although crude way to communicate between sessions.

David Kristensen
  • 4,290
  • 3
  • 14
  • 7