Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------Dependencies------------------
- var app = require("express")();
- var pgsql = require("pg");
- var http = require('http').Server(app);
- var io = require("socket.io")(http);
- //------------------------------------------------
- const con = new pgsql.Pool({
- user: 'postgres',
- host: 'localhost',
- database: 'fbstatus',
- password: 'postgres',
- port: '5432'
- });
- app.get("/",function(req,res){
- res.sendFile(__dirname + '/index.html');
- });
- http.listen(3000, function(){
- console.log('listening on *:3000');
- });
- // Connect to Postgres
- con.connect(function(err, client) {
- if(err) {
- console.log(err);
- }
- io.sockets.on('connection', function (socket) {
- // Listen for all pg_notify channel messages
- client.on('notification', function(msg) {
- console.log(msg);
- socket.emit('update', msg);
- });
- // Designate which channels we are listening on. Add additional
- channels with multiple lines.
- client.query('LISTEN notification_events');
- });
- });
- <!DOCTYPE html>
- <html>
- <body>
- <h1>real time data recieved from server</h1>
- <p>When you insert a new row into database, the below p tag
- content will be changed</p>
- <p id="socketio"></p>
- </body>
- <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
- <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
- <script>
- var socket = io.connect('http://localhost');
- socket.on('update', function (data) {
- var data_got=JSON.parse(data.payload)
- console.log(data_got["data"]["id"]);
- document.getElementById("socketio").innerHTML = data.payload;
- });
- </script>
- </html>
Add Comment
Please, Sign In to add comment