mysql - Socket.io and Phalcon PHP -


i'm using phalcon php , want try socket.io first time. did tutorial chat message socket.io. want select data in database count number of rows in table 'product' query phalcon :

$count_products = product::count(); 

for example in html page have 5 products , when i'll add 1 product or more table product want auto refresh see 6 products in html page.

could me ?

once using sockets on ajax requests, should keep phalcon , try implement simple tool using node.js+socket.io.

simplest approach create in node event forwarder listen on events on 1 side, , forward them users browser. more described here.

in case recommend add phalcon model aftersave listener described in documentation. during aftersave method able pass eg. udp packet node service information, there new records in x table.

than node service should forward event clients' browsers, javascript should decide, if x table being viewed current user. if is, should lock view prevent actions , fire ajax request phalcon service, should ask db actual data , refresh html content within view.

of course can make node service listen changes directly on db , send proper events proper users content prepared update dynamically. advanced approach , may bit overkilling solution. i'd stress out here, proposed solution work in fractions of second, giving real-time feeling, long phalcon services optimised speed , data sent not big.

of course can create forwarding service using phalcon instead of node, after time regret it, easier maintain such event-based script using event-based tools. advanced php programmers work javascript time time, not challenge learn such small bit of note.js make such simple solution.

sending package on socket php

$host = array(     'scheme' => 'udp', // udp makes lightweight , connectionless     'host' => '192.168.10.10' // choose ip node running     'port' => '8888' // choose 1 > 1023 ); $param = sprintf('%s://%s:%s', $host['scheme'], $host['host'], $host['port']); $socket = fsockopen($param, $errno, $errstr, $timeout); 

i have enclosed in class socket. i'd rather using persistent socket (pfsockopen), few processes @ 1 time use 1 socket if looking otherwise. in aftersave using send method, more or less:

fwrite($socket, json_encode($msg)); 

node.js event forwarding script on websocket

exampleous configuration file node.json:

{     "wsserver": {         "listeners": {             "udp": {                 "port": 8888             }         },         "server": {             "port": 8000         }     } } 

your dependencies socket.io , dgram. know later mean, once learn how create node application.

to make easier understand whats following:

var config = require('node.json').wsserver; var app = require('http').createserver().listen(config.server.port); var io = require('socket.io').listen(app); var listenerdgram = require('dgram').createsocket('udp4'); listenerdgram.bind(config.listeners.udp.port);  var users = {};  io.sockets.on('connection', function(socket) {     // if make user connect individual id during     // websocket connection, providen after ?, ?1234     var user = parseint(socket.handshake.query.user);      // here save user var users     if (!users[user]) {         users[user] = {             sockets: [socket]         };     } else {         users[user].sockets.push(socket);     }      socket.on('disconnect', function() {         // removing user var users         // warning: socket socket, , if last         //   socket closed, remove whole user section     });  });  // emit data sockets of choosen users emit = function(sockets, message, data) {     (var x in sockets) {         if (sockets[x]) {             sockets[x].emit(message, data);         }     } };  // udp listening section listenerdgram.on('message', function(msg, rinfo) {      // can declare checkincoming if have standarized     // frames, or use msg     var _dat = checkincoming(msg.tostring().trim(), true);     var _response = {         action: _dat.action,         data: _dat.data     };      // can declare standarizeframe define own protocol,     // or use _response.     var frame = standarizeframe(_response);      if (_dat.user) { // emitting single user declared         emit(users[_dat.user].sockets, 'notification', frame);     } else { // emitting connected users         io.emit('notification', frame);     } }); 

so send jsoned string server node running on port 8888, , users web interfaces connects same host on port 8000 receive them back. lots of debugging ahead of you.

ps: sorry such arbitrary answer, walked on myself, i'm in love phalcon , have had no knowledge node.js exist. giving myself time learn node helped me reduce event based solutions absolute minimum, enclosing them withing 100 lines instead of thousands, made them easy maintain, gave servers deep breath on cpu , satisfied client more php-based ones.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -