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

Send Message Via Nodemon #1519

Closed
nahtnam opened this issue Feb 3, 2019 · 17 comments
Closed

Send Message Via Nodemon #1519

nahtnam opened this issue Feb 3, 2019 · 17 comments
Labels
feature request stale no activity for 2 weeks

Comments

@nahtnam
Copy link

nahtnam commented Feb 3, 2019

  • nodemon -v:1.18.9
  • node -v:10.14.2
  • Operating system/terminal environment: MacOS High Sierra
  • Using Docker? What image: No
  • Command you ran: Used required version of nodemon

So I have a weird/interesting use case of nodemon. I want to conditionally restart the server (which is possible), however the times I don't want to restart the server, I would like to send a message/event to the child process/instance of my code.

Expected behaviour

I have looked at the docs and skimmed through the source code, I don't think its possible.

Actual behaviour

Nothing happens

Steps to reproduce

Here is the code I use:

const server = nodemon({
  watch: ['src'],
  ext, // assume .js
  ignore: [path.join(cwd, `./src/routes/**/*.${ext}`)], // i am excluding this folder so I can conditionally restart
  exec, // node src/index.js
});

// this is fine
server.on('start', function () {
  console.log('nodemon started');
}).on('crash', function () {
  console.log('script crashed for some reason');
}).on('restart', () => {
  console.log('nodemon is restarting')
})

chokidar.watch(
  path.join(cwd, './src/routes', `**/*.js`),
  {
    ignored: /(^|[\/\\])\../, // ignore dot files
  }
).on('change', (path) => {
  if (something) {
    nodemon.restart(); // great
  } else {
    nodemon.send('message', 'dont restart but do this'); // UNIMPLEMENTED/HOW DO I DO THIS?
  }
});

One possible solution is to give access to the child_process that nodemon opens up.


If applicable, please append the --dump flag on your command and include the output here ensuring to remove any sensitive/personal details or tokens.

@stale
Copy link

stale bot commented Feb 20, 2019

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@stale stale bot added the stale no activity for 2 weeks label Feb 20, 2019
@nahtnam
Copy link
Author

nahtnam commented Feb 20, 2019

Bump!

@stale stale bot removed the stale no activity for 2 weeks label Feb 20, 2019
@remy
Copy link
Owner

remy commented Feb 20, 2019

By "send message" - can you be more specific? What exactly do you want to send?

@nahtnam
Copy link
Author

nahtnam commented Feb 20, 2019

@remy Kind of like emitting events to a child process. https://medium.com/@NorbertdeLangen/communicating-between-nodejs-processes-4e68be42b917 Something like this but the events should go to my server such that I can tell it to hot swap the module instead of restarting

@stale
Copy link

stale bot commented Mar 6, 2019

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@stale stale bot added the stale no activity for 2 weeks label Mar 6, 2019
@nahtnam
Copy link
Author

nahtnam commented Mar 6, 2019

Bump!

@stale stale bot removed the stale no activity for 2 weeks label Mar 6, 2019
@nahtnam
Copy link
Author

nahtnam commented Mar 18, 2019

@remy Any thoughts on this issue?

@remy
Copy link
Owner

remy commented Mar 18, 2019

Your original reply didn't really answer my question.

nodemon.send('message', 'dont restart but do this'); // UNIMPLEMENTED/HOW DO I DO THIS?

nodemon supports postMessage for RPC, but your slim example is two things: 1) don't restart, 2) "this"

What is "this" supposed to be. Can you provide an example of exactly what you would like to do?

@nahtnam
Copy link
Author

nahtnam commented Mar 23, 2019

@remy

Sorry for the late response. So here is my use case: I am working on a "framework"/library for my app where it will hot-swap certain modules so the server doesn't have to restart (boot-time is a few seconds) only in development mode. I would however like to use nodemon to restart the server if any of the files that are not in that specific folder change.

Here is how I start the server in my CLI program:

const server = nodemon({
  watch: ['src'],
  ext, // assume .js
  ignore: [path.join(cwd, `./src/routes/**/*.${ext}`)], // i am excluding this folder so I can conditionally restart
  exec: 'node src/index.js',
});

Notice how I ignore the routes folder. I then set up chokidar to watch the routes folder manually:

chokidar.watch(
  path.join(cwd, './src/routes', `**/*.js`),
  {
    ignored: /(^|[\/\\])\../, // ignore dot files
  }
).on('change', (path) => {
  if (shouldFullyRestartServer) {
    nodemon.restart(); // great
  } else {
    # HERE
  }
});

Where it says HERE, I want to tell the child process which is running node src/index.js to emit a "swapModule" event or something which would trigger the child app to swap out the modules.

Maybe there is a simpler solution that I can't see, but basically, I would like the ability for the parent process running nodemon via script to be able to send messages to the child process that nodemon starts.

Thanks!

@stale
Copy link

stale bot commented Apr 6, 2019

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@stale stale bot added the stale no activity for 2 weeks label Apr 6, 2019
@remy
Copy link
Owner

remy commented Apr 8, 2019

Okay, I can follow your code example. You want to push the message directly into the child but it needs to go via nodemon. I think I can see how that would work.

I'll have to come back to you (though the stalebot will keep trying to close this issue - sorry!)

@stale stale bot removed the stale no activity for 2 weeks label Apr 8, 2019
@nahtnam
Copy link
Author

nahtnam commented Apr 8, 2019

Hey!

Thank you! I think the opposite should also work (and I might need it for that specific use case). If the child emits an event, nodemon could capture it and add a "event" hook or something that the parent can listen on.

Thanks,
@nahtnam

@stale
Copy link

stale bot commented Apr 22, 2019

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@stale stale bot added the stale no activity for 2 weeks label Apr 22, 2019
@nahtnam
Copy link
Author

nahtnam commented Apr 22, 2019

bump!

@stale stale bot removed the stale no activity for 2 weeks label Apr 22, 2019
forivall added a commit to forivall/nodemon that referenced this issue Apr 23, 2019
add event to listen to messages coming from the child's ipc events, partially implementing remy#1519
remy pushed a commit that referenced this issue May 1, 2019
add event to listen to messages coming from the child's ipc events, partially implementing #1519
@stale
Copy link

stale bot commented May 6, 2019

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@stale stale bot added the stale no activity for 2 weeks label May 6, 2019
@nahtnam
Copy link
Author

nahtnam commented May 7, 2019

Bump (because it looks like there is some progress being made).

Unfortunately I don't need this feature anymore (I ended up with a better solution) but hopefully, someone finds this useful. Thank you!

@stale stale bot removed the stale no activity for 2 weeks label May 7, 2019
@stale
Copy link

stale bot commented May 21, 2019

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@stale stale bot added the stale no activity for 2 weeks label May 21, 2019
@stale stale bot closed this as completed May 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request stale no activity for 2 weeks
Projects
None yet
Development

No branches or pull requests

2 participants