Search code examples
node.jsherokusocket.io

heroku node js + socket.io listion to POST from php


I have a node.js server at heroku side this is working on localhost but when I commit to heroku the app crash.

   'use strict';

const express = require('express');
const socketIO = require('socket.io');
const path = require('path');

const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, 'index.html');

const server = express()
  .use((req, res) => res.sendFile(INDEX) )
  .listen(PORT, () => console.log(`Listening on ${ PORT }`));

const io = socketIO(server);

io.on('connection', (socket) => {
  console.log('Client connected');
  socket.on('disconnect', () => console.log('Client disconnected'));
});


var app = express();
app.set('port', (process.env.PORT || 5000));

var bodyParser = require('body-parser')
app.use(bodyParser.json());

app.use(express.static(__dirname + '/public'))

app.post('/phpcallback', function(req, res) {
    var content = req.body;
    console.log('message received from php: ' + content.msg);
    //to-do: forward the message to the connected nodes.
    res.end(content.msg);
});

app.listen(app.get('port'), function() {
  console.log("Node app is running at localhost:" + app.get('port'))
});

the app need to listen for POST request that come form my PHP server and then emit to user at js client side, the code working good when its on localhost but when I commit and push to heroku the app crashed. anyone know what is the problem? i am new to node.js and socket.io


Solution

  • Try like this (improved syntax and general usage)

    var express = require('express');
    var io = require('socket.io');
    var path = require('path');
    var PORT = process.env.PORT || 3000;
    var INDEX = path.join(__dirname, 'index.html');
    // Express & Middleware
    var app = express();
    var bodyParser = require('body-parser');
    app.use(bodyParser.json());
    // SocketIO
    var server = require('http').createServer(app);
    var io = require('socket.io')(server);
    io.on('connection', function(){
      console.log('[socket]','client connected');
      socket.on('disconnect', () => console.log('[socket]','client disconnected'));
    });
    server.listen(PORT,function(){console.log("[SERVER] listening @ port:",PORT);});
    //GET Routes
    app.use(express.static(__dirname + '/public'))
    app.get('/',function(req,res){res.sendFile(INDEX);});
    //POST Routes
    app.post('/phpcallback', function(req, res) {
      var content = req.body;
      if(content.msg){
        console.log('message received from php: ' + content.msg); //
        /*to-do: forward the message to the connected nodes.*/
        //io.sockets.on('forward',content.msg); //forward to ALL connected socket.io clients (requires client-side event)
        res.send(content.msg); //forward response with content.msg to the original request (only) of this route.
      }else{console.log('[/phpcallback] : missing message (content.msg)');}
    });
    

    I saw your to-do, if you want to forward a message to all connected clients socket.io :

    io.sockets.emit('your_event','your_message');
    

    This might come handy : SocketIO Emit CheatSheet