N
N
nepster-web2014-06-17 15:52:56
PHP
nepster-web, 2014-06-17 15:52:56

What is the logic of the realtime application?

I make my own project, study, don’t touch anyone, and then for no reason I needed real time. I dug through a bunch of docs on this topic, found 100500 disputes on the topic, in general, I have a lot of information from which I cannot understand which is better and how I should use it.

First of all, I came across this article habrahabr.ru/post/162301/, where the author talks about his site and a page that needs to be constantly updated. I need the same, only on the example of other gaming tables. For example, we have a fool game, users create applications or join them. You also need to constantly refresh the page and show the content.

For myself, I have already chosen the following tools: php, node.js, sockjs, redis.

Now I have questions about how this should all work, unfortunately, in that article the author showed almost nothing from the code and explained little.

Question 1. Did I understand correctly how to build the architecture of this case?:
client - receives data and renders the page

<script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>

<script type="text/javascript">

    var sock = new SockJS('http://127.0.0.1:9999/echo');
    
    sock.onopen = function() {
       console.log('open');
    };
    
    
    sock.onmessage = function(e) {
       
       var data = JSON.parse(e.data);
       
       console.log(data)
       // Парсим
       
    };
        
    sock.onclose = function() {
       console.log('close');
    };

</script>


node.js server , get data from redis and send to client
var http   = require('http');
var sockjs = require('sockjs');
var redis  = require('redis');
var client = redis.createClient();

client.subscribe("chan-1");

var echo = sockjs.createServer();


echo.on('connection', function(conn) {
    
    /*setInterval(function(){
          

    }, 1000);*/

    
    
    client.on('message', function(channel, message) {

        console.log(channel);
        console.log(message);
        console.log('---------------');
        
        conn.write(message);
        
    });
    
    
    conn.on('close', function() {});
});




var server = http.createServer();
echo.installHandlers(server, {prefix:'/echo'});
server.listen(9999, '0.0.0.0');


And actually the most important, php that works with the data and throws them into the channel:
$redis = new Redis();
$redis->pconnect('localhost', 6379);
$redis->publish("chan-1", json_encode(['test'=>'test']));


Question 2 how to handle data correctly in php ?:

For example, a user creates an application for a game, as I understand it, it is necessary to send the data of a new application to the channel?

$redis->publish("chan-1", json_encode(['newGame'=>$data]));


Also, if, for example, the user has joined the application, it is necessary to send data to refresh the page:
$redis->publish("chan-1", json_encode(['updateGame'=>$data]));


Well, if the game has started, you need to remove the application:
$redis->publish("chan-1", json_encode(['endGame'=>$id]));


It turns out, as I understand it, for each such user action, you need to send data to the channel. Is it correct?

Question 3 How to render the page correctly?:
The page is a table that will constantly change (new applications will appear, old ones will disappear, and the data in the applications may change, for example, players will be added). Do you need to redraw the table from 0 each time, or work with specific parts of it? How do memory costs occur in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Snegirev, 2014-06-17
@Rikkit

And my question number 1 is why php if there is nodejs?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question