Answer the question
In order to leave comments, you need to log in
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>
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');
$redis = new Redis();
$redis->pconnect('localhost', 6379);
$redis->publish("chan-1", json_encode(['test'=>'test']));
$redis->publish("chan-1", json_encode(['newGame'=>$data]));
$redis->publish("chan-1", json_encode(['updateGame'=>$data]));
$redis->publish("chan-1", json_encode(['endGame'=>$id]));
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question