Insert tweets into mysql DB and display tweets into web browser

This Flow is store tweets into MySql DB and display into web browser. Require Node: 1) Twitter Node. 2) Function Node 3) http request Node 4) http response Node 5) json Node 6) template Mustache template Node 7) MySql Node use [npm install node-red-node-mysql] for installation 8) comment Node.

Note: You have to authorised your twitter account.

[{"id":"2bd189c6.b3e366","type":"twitter in","z":"465ca073.d6a508","twitter":"","tags":"#Budget2017","user":"dm","name":"Budget 2017","topic":"tweets","inputs":0,"x":100,"y":1793,"wires":[["693e43f7.e4f56c"]]},{"id":"4832d6e0.040c48","type":"mysql","z":"465ca073.d6a508","mydb":"7ec1a51d.fb508c","name":"mydatabase","x":398,"y":2055,"wires":[["6a26250d.259fac"]]},{"id":"693e43f7.e4f56c","type":"function","z":"465ca073.d6a508","name":"insert query","func":"var str = msg.payload;\n\nstr = str.replace(/'/g, '');\n\nmsg.topic = \"insert into tweets(`content`,`tweets_date`) values(QUOTE('\" + str + \"'),NOW())\";\nreturn msg;","outputs":1,"noerr":0,"x":197,"y":1885,"wires":[["4832d6e0.040c48"]]},{"id":"d581aa8a.8ed8f8","type":"http in","z":"465ca073.d6a508","name":"tweets_listing_request","url":"/tweetslist","method":"get","swaggerDoc":"","x":121,"y":2052,"wires":[["ed42b65a.082f5"]]},{"id":"ed42b65a.082f5","type":"function","z":"465ca073.d6a508","name":"getTweets","func":"msg.topic = \"select id, content, tweets_date from tweets order by id desc limit 25\";\nreturn msg;","outputs":1,"noerr":0,"x":144,"y":2145,"wires":[["4832d6e0.040c48"]]},{"id":"c515c168.f5b368","type":"http response","z":"465ca073.d6a508","name":"http_tweets_response","x":638,"y":2055,"wires":[]},{"id":"be4de30.501492","type":"comment","z":"465ca073.d6a508","name":"Example for MySQL Database","info":"Here i am using MySQL for inserting records into it and fetching data and display it into web browser.","x":151.5,"y":1983,"wires":[]},{"id":"8c86b027.c3f13","type":"template","z":"465ca073.d6a508","name":"html","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n  <head>\n    <title>My Tweets from MySQL</title>\n    <meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\">\n    <meta charset=\"utf-8\">\n  </head>\n  {{{payload}}}\n</body>\n</html>\n","x":554,"y":2143,"wires":[["c515c168.f5b368"]]},{"id":"6a26250d.259fac","type":"json","z":"465ca073.d6a508","name":"","x":395,"y":2143,"wires":[["89e994f.91f58e8"]]},{"id":"89e994f.91f58e8","type":"function","z":"465ca073.d6a508","name":"tweet_func","func":"var json = JSON.parse(msg.payload);\nvar html = '<table border=\"1\"><tr><th>ID</th><th>Tweets</th><th>Date</th></tr>';\nhtml =json.reduce( function(prevVal, currentVal, currIndex, arr) {\n return  prevVal+\"<tr><td>\"+currentVal.id + \"</td><td>\" + currentVal.content+\"</td><td>\" + currentVal.tweets_date+\"</td></tr>\";\n}, html);\nhtml += '</table>';\n//json.setEncoding('utf8');\nmsg.payload=html;\nreturn msg;\n","outputs":1,"noerr":0,"x":481,"y":2218,"wires":[["8c86b027.c3f13"]]},{"id":"7ec1a51d.fb508c","type":"MySQLdatabase","z":"","host":"127.0.0.1","port":"3306","db":"petals","tz":""}]
toshunigam

Flow Info

created 2 months ago

Node Types

Core
  • comment (x1)
  • function (x3)
  • http in (x1)
  • http response (x1)
  • json (x1)
  • template (x1)
  • twitter in (x1)
Other

Tags

  • MySql
  • Twitter
  • Listing
  • Browser
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option