Guest User

Untitled

a guest
Jan 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. //-----------------Dependencies------------------
  2. var app = require("express")();
  3. var pgsql = require("pg");
  4. var http = require('http').Server(app);
  5. var io = require("socket.io")(http);
  6. //------------------------------------------------
  7.  
  8. const con = new pgsql.Pool({
  9. user: 'postgres',
  10. host: 'localhost',
  11. database: 'fbstatus',
  12. password: 'postgres',
  13. port: '5432'
  14. });
  15.  
  16. app.get("/",function(req,res){
  17. res.sendFile(__dirname + '/index.html');
  18. });
  19.  
  20. http.listen(3000, function(){
  21. console.log('listening on *:3000');
  22. });
  23.  
  24. // Connect to Postgres
  25. con.connect(function(err, client) {
  26. if(err) {
  27. console.log(err);
  28. }
  29.  
  30. io.sockets.on('connection', function (socket) {
  31.  
  32. // Listen for all pg_notify channel messages
  33. client.on('notification', function(msg) {
  34. console.log(msg);
  35. socket.emit('update', msg);
  36. });
  37.  
  38. // Designate which channels we are listening on. Add additional
  39. channels with multiple lines.
  40. client.query('LISTEN notification_events');
  41. });
  42. });
  43.  
  44. <!DOCTYPE html>
  45. <html>
  46. <body>
  47. <h1>real time data recieved from server</h1>
  48. <p>When you insert a new row into database, the below p tag
  49. content will be changed</p>
  50. <p id="socketio"></p>
  51. </body>
  52. <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
  53. <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
  54. <script>
  55. var socket = io.connect('http://localhost');
  56. socket.on('update', function (data) {
  57. var data_got=JSON.parse(data.payload)
  58. console.log(data_got["data"]["id"]);
  59. document.getElementById("socketio").innerHTML = data.payload;
  60. });
  61. </script>
  62. </html>
Add Comment
Please, Sign In to add comment