Nodejs how to update client after external program has returned?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A Node.js server can wait for an external program to finish without blocking the event loop, but the client still needs a way to hear about the result. The real design question is not how to "pause" the browser. It is how the server should notify the client after the child process exits or returns output.
Run the External Program Asynchronously
In Node.js, external commands are usually started with the child_process module. The server can keep handling other work while the child runs.
That gives the server a promise that resolves when the external program finishes.
Push the Result to the Client With WebSockets
If the user should see the result immediately, WebSockets are a strong fit. The browser opens a persistent connection, the server starts the external program, and the server emits an event when the process ends.
Server example with Socket.IO:
Client example:
This pattern is clean because the client does not need to guess when the external process is done.
Use HTTP Plus Polling When Real-Time Push Is Unnecessary
If you do not want persistent sockets, another common design is:
- client sends an HTTP request to start a job
- server returns a job ID immediately
- client polls a status endpoint until the job is complete
That looks like this on the server:
Polling is simpler to deploy than WebSockets, but it adds delay and repeated requests.
Do Not Hold the Original HTTP Request Open Unless the Job Is Short
For very short commands, it can be fine to wait and return the output directly in one HTTP response:
That is the simplest option when the external program completes quickly. For long-running jobs, keeping the request open is brittle and often leads to timeouts or a poor user experience.
Choose the Notification Method Based on Job Duration
A practical rule is:
- if the external program finishes quickly, return the result in the same HTTP request
- if completion time is unpredictable, use WebSockets or job polling
- if many clients must observe the same job, store status centrally and let clients subscribe or poll
The notification pattern should match the latency and UX requirements of the feature.
Common Pitfalls
The biggest mistake is trying to write to the client from code that no longer has a valid response object or connection strategy. Another is blocking the event loop with synchronous process execution when an async child process would work better. Developers also keep long HTTP requests open for jobs that can run for minutes, which leads to timeouts and brittle reconnect behavior. If the result matters after the page is reloaded, you also need some persistent job state instead of only an in-memory callback chain.
Summary
- Run the external program asynchronously with
child_process. - Decide separately how the client will learn that the job finished.
- Use WebSockets for immediate server push.
- Use polling with a job ID when sockets are unnecessary or inconvenient.
- Return the result in the original HTTP response only when the external job is short.
- Store job state explicitly if the client may disconnect or reconnect later.

