Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "listening" or "ready" event #832

Closed
pensierinmusica opened this issue May 14, 2016 · 9 comments
Closed

Add "listening" or "ready" event #832

pensierinmusica opened this issue May 14, 2016 · 9 comments

Comments

@pensierinmusica
Copy link
Contributor

Hi @remy!

Would you be so kind to add the listening event to nodemon states?

You can get it directly from Node: https://nodejs.org/api/net.html#net_event_listening

This is needed for example when using gulp-nodemon, since starting the Node server can be part of multiple tasks and you don't want to run something like browsersync until the server is actually listening.

I tried using the start event, but when the browser loads the server is not listening yet, so you get a blank page and need to manually reload, which is annoying.

Thanks in advance for adding this!

Cheers

@remy
Copy link
Owner

remy commented May 14, 2016

Urm...but nodemon doesn't listen. It doesn't even use the net module.

@pensierinmusica
Copy link
Contributor Author

Mmm, I see. Is there a way then for the app code to interact with Nodemon and decide when to trigger some ready event that could be integrated in a Gulp task?

@pensierinmusica pensierinmusica changed the title Add "listening" event Add "listening" or "ready" event May 18, 2016
@gggauravgandhi
Copy link

Share your gulp file or integration of gulp-nodemon

@pensierinmusica
Copy link
Contributor Author

Hi,

This is the way I wish it could work:

// Start server
gulp.task('server', function (cb) {
  $.nodemon({
    script: serverPath,
    watch: serverPath
  })
  .on('ready', cb);
});

But for now I'm stuck with a less elegant solution:

// Start server
gulp.task('server', function (cb) {
  $.nodemon({
    script: serverPath,
    watch: serverPath
  })
  .on('start', function () {
    setTimeout(cb, 500);
  });
});

@vladkosinov
Copy link

@pensierinmusica you can parse stdout and trigger event (call callback) on specific message:

gulp.task('server', function (cb) {
    const readyMessage = 'server started';

    nodemon({
        script: serverPath,
        watch: serverPath,
        stdout: false
    })
    .on('stdout', function (stdout) {
        console.log('server stdout:', stdout.toString());

        const isReady = stdout.toString().includes(readyMessage);
        if (!isReady) { return; }
        cb();
    });    
});

@vladkosinov
Copy link

@remy close? :)

@sandro-pasquali
Copy link

@pensierinmusica for my case I found it useful to simply start browsersync from the server itself. So, child_process.exec('gulp <post-start-task-chain-head>') at the point when you know your server is ready (say, listen).

@RichardsonWTR
Copy link

I am having a similar issue. I reported in Browsersync repo.
Can anyone help me on this?

@pensierinmusica
Copy link
Contributor Author

Thanks @vlkosinov and @sandro-pasquali for your suggestions! I ended up adopting a modified version of what @vlkosinov suggests. @sandro-pasquali method also looks nice, but decided to keep the logic of Gulp inside gulpfile.js, and not split with the main server script.

So this is the solution I adopted:

gulp.task('server', function (cb) {
  $.nodemon({
    script: serverPath,
    watch: serverPath,
    stdout: false
  })
  .on('stdout', function (mess) {
    console.log(mess.toString());
    cb();
  })
  .on('stderr', function (err) {
    console.err(err.toString());
  });
});

I guess we can close this issue now, thanks everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants