javascript - Hapi.js + Socket.io together -


i want run hapi.js socket.io. great if had separate connections socket.io , hapi app using same server because want use hapi auth cookies in socket

i tried few solutions, none of them working , server crashing. tried run socket.io on same port hapi , app started, got "this localhost page can not found" error. did wrong? appreciated

here's code:

const hapi = require('hapi'); const server = new hapi.server(); const config = require('./config/config.js'); const port = number(process.env.port || 3000); const io = require("socket.io")(port);  server.connection({     port: port });  // routes here...  io.on("connection", function (socket) {      console.log('connected');      // socket stuff here.  })  server.start(function(err) {      if (err) {         console.error(err);         throw err;     }      console.log('server started @ %s', server.info.uri);  }); 

i solved problem, creating 2 separate connections. working great!

here's how code looks now:

const hapi = require('hapi'); const server = new hapi.server(); const config = require('./config/config.js'); const port = number(process.env.port || 3000);  server.connection({ port: port, labels: ['app'] }); server.connection({ port: 8000, labels: ['chat'] });  const app = server.select('app');  app.register([     // app's stuff goes here ]);  var io = require('socket.io')(server.select('chat').listener);  io.on("connection", function (socket) {      console.log('connected');      // socket stuff here.  })  server.start(function(err) {      if (err) {         console.error(err);         throw err;     }      console.log('server started');  }); 

that website helped me lot


Comments