Advertisement
Guest User

Untitled

a guest
Aug 31st, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. //Nodejs Server ---------------------------
  2. var io = require('socket.io').listen(3000);
  3. var redis = require('redis');
  4. var $clients = {};
  5.  
  6. var $notifySubscribe = redis.createClient();
  7. var $loginUserSubscribe = redis.createClient();
  8. $notifySubscribe.subscribe('notify');
  9. $loginUserSubscribe.subscribe('loginUser');
  10.  
  11. $notifySubscribe.on('message', function($channel, $message) {
  12. var $data = JSON.parse($message);
  13. if($data.userId in $clients){
  14. $clients[$data.userId].emit('updateNotifications', '+1');
  15. }
  16. });
  17.  
  18. io.sockets.on('connection', function(socket) {
  19.  
  20. $loginUserSubscribe.on('message', function($channel, $message){
  21. var $data = JSON.parse($message);
  22. if(!($data.userId in $clients)){
  23. $clients[$data.userId] = socket;
  24. }
  25. });
  26.  
  27. });
  28.  
  29. //Client ----------------------------------
  30. socketIo.on('updateNotifications', function($count){
  31. alert($count);
  32. });
  33.  
  34. //Php Server ------------------------------
  35. $data = array('userId' => 20584, 'username' => 'mohammadsaleh');
  36. $this->__redis->publish("notify", json_encode($data));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement