gruntjs / grunt-contrib-connect

Start a static web server.
http://gruntjs.com
MIT License
714 stars 146 forks source link

Is it possible to use livereload with socket.io? #199

Closed ghost closed 8 years ago

ghost commented 8 years ago

I'm currently having a project that runs with grunt serve command. serve is defined like this in my Gruntfile.js

grunt.registerTask('serve', function (target) {
    if (target === 'dist') {
      return grunt.task.run(['build', 'connect:dist:keepalive']);
    }

    grunt.task.run([
      'clean:server',
      'bower-install',
      'concurrent:server',
      'configureProxies:server',
      'autoprefixer',
      'connect:livereload',
      'watch'
    ]);
  });

I setup socket.io using onCreateServer function like this (server options under connect object):

  options: {
    port: 9000,
    hostname: 'localhost',
    livereload: 35729, //was 35729
    onCreateServer: function(server, connect, options){
        var io = require('socket.io').listen(server);
        console.log("Hello World!");
    }
  },

According to the grunt-contrib-connect guide when using onCreateServer function you should run this with grunt connect command, so if I add connect in my grunt serve task like this:

grunt.registerTask('serve', function (target) {
    if (target === 'dist') {
      return grunt.task.run(['build', 'connect:dist:keepalive']);
    }

    grunt.task.run([
      'clean:server',
      'bower-install',
      'concurrent:server',
      'configureProxies:server',
      'autoprefixer',
      'connect:livereload',
      'connect',
      'watch'
    ]);
  });

It says that "Fatal error: Port 9000 is already in use by another process.". So I want to know is it possible to use socket.io with livereload or not? If yes what's good practice to do it?

ghost commented 8 years ago

Okay, found the solution. It seemed my code was correct and socket.io at onCreateServer function can actually listen properly the server that is running.