taufikmas

pubnub to mysql

Jun 20th, 2017
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ---------------------------------------------------------------------------
  2.  
  3.     Init PubNub and Get your PubNub API Keys:
  4.     http://www.pubnub.com/account#api-keys ./node.js/pubnub.js
  5.  
  6. --------------------------------------------------------------------------- */
  7.  
  8. var pubnub = require("./node.js/pubnub.js").init({
  9.     publish_key   : "pub-cxxxxxxxxxxxxx",
  10.     subscribe_key : "sub-c-xxxxxxxxxxxxxxxxxxxx"
  11. });
  12. var mysql      = require('mysql');
  13. var connection = mysql.createConnection({
  14.   host     : 'localhost',
  15.   user     : 'root',
  16.   password : '',
  17.   database : 'pbss'
  18.   //debug: false
  19. });
  20.  
  21. /* ---------------------------------------------------------------------------
  22. Listen for Messages
  23. --------------------------------------------------------------------------- */
  24.  
  25.  
  26.  
  27. function subscribe(channel) {
  28.     pubnub.subscribe({
  29.         'channel' : channel,
  30.         'connect' : function(c) {
  31.             console.log('CONNECTED to ' + c);
  32.         },
  33.         'disconnect' : function(c) {
  34.             console.log('CONNECTED to ' + c);
  35.         },
  36.         'reconnect' : function(c) {
  37.             console.log('CONNECTED to ' + c);
  38.         },
  39.         'error' : function(e) {
  40.             console.log('ERROR  ' + JSON.stringify(r));
  41.         },
  42.         'callback' : function(m,a,subscribed_channel,c,real_channel) {
  43.             var dataku = JSON.stringify(m); ; //"ALERT!HYK0009-2121"
  44.             console.log(dataku);
  45.             var datamasuk=JSON.stringify(m); //"ALERT!HYK0009"
  46.             console.log(datamasuk);
  47.             var statusupd="";
  48.             var datamasuk = datamasuk.substring(1, datamasuk.length-1); //ALERT!HYK0009
  49.             console.log("datamasuk "+datamasuk);
  50.            
  51.             var isine = datamasuk.slice(0, 5); //ALERT
  52.             console.log("isine "+isine);
  53.            
  54.             var pengirim = datamasuk.substring((datamasuk.search("!")+1), datamasuk.search("-")); //HYK0009
  55.             console.log("pengirim "+pengirim);
  56.             var d = new Date();
  57.             var waktune = d.toLocaleString();
  58.    
  59.            
  60.             connection.connect(function(err) {
  61.              // if (err) throw err;
  62.              // console.log("Connected!");
  63.              
  64.               var sql = "INSERT INTO inboxpub (pengirim, data) VALUES?";
  65.               var values=[[pengirim, datamasuk]];
  66.               connection.query(sql,[values], function (err, result) {
  67.                 if (err) throw err;
  68.                  console.log("Number of array records inserted: " + result.affectedRows);
  69.               });
  70.              
  71.               //var sql = "UPDATE client SET notelp = ? WHERE idclient=?";
  72.               //var valuesnya=[[pengirim]];
  73.               connection.query('UPDATE client SET status = ? WHERE idclient=?', [isine,pengirim],function (err, result){
  74.                 if (err) throw err;
  75.                 console.log(result.affectedRows + " record(s) updated");
  76.               });
  77.        
  78.             });
  79.             //connection.end();
  80.            
  81.         }
  82.     })
  83.     //console.log(process.argv.slice(2));
  84.  
  85. }
  86. subscribe("Channel-YOGYA");
RAW Paste Data