I
I
Ivan Driuk2015-05-13 16:45:10
Socket.io
Ivan Driuk, 2015-05-13 16:45:10

How to deploy Hapi + Socket.io example on Heroku?

There is a sample code that works well on a local machine.

var Hapi = require('hapi'),
    server = new Hapi.Server();

server.connection({ host: process.env.HOSTNAME || 'localhost',
                    address: process.env.IP || '0.0.0.0',
                    port: Number(process.env.PORT || 3000)
                  });


server.views( {
  engines: {
    ejs: require('ejs')
  },
    relativeTo: __dirname,
    path: 'templates'
  }
);

server.route({
  method: 'GET',
  path: '/',
  handler: function(request, reply) {
    reply.view('index');
  }
});

var io = require('socket.io')(server.listener);

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});


server.start(function() {
  console.log('Listening on', server.info.uri);
});

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io();
      $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
      });
    </script>
  </body>
</html>

repo on github
What is missing to make it work on heroku?
Maybe there are some errors? Or do I need some settings somewhere or something else? Does anyone have a good example where you can just git push heroku master and it will work fine? Tell me please.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question