AI Bot Challenges - Node JS - Hung up - Understand the root cause before web search.

To solve a problem is easy with different forums and google search. But it is equally important to understand the reason why the problem happened and how the line of code or instructions from forums made it work

Use case where this article is useful.

  • You are using Node JS server and you get server hung up error.
  • You get an error from event.js stating that there is unhandled error
  • You want to understand the concept, why it happened, and not just copy the line of code from web and resolve the problem.

Solutions that you will find on web will tell you one of the below.

The solution to this problem is available in many forums in different ways and you can definitely find via google search.
Common methods or reasons explained to resolve this problem are
  • Handle the process on error method , for the people using the express framework.
  • Do a request.end  or response.end to make sure there is no request that is hung and finally leading the server to hung up state.
  • See if there are existing processes running on the same port, kill the process and restart your server.
  • Make the version of node js to be the latest in your package.json file.

One step back : What happened, that caused this situation? What is the root cause, before I fix it?

I do not understand complex things, so I will keep it very simple for people to understand the basic reason for hung up.
If you are using some external services in your Node JS e.g. calling a Twilio service to send SMS to the end user, or some other service, you have more probability to get this error, and the reason is below.
  • When your server web-hook is called from an User chatting on a platform like FB --> AI platform a "Request" is triggered, lets name is  "http-req1"
  • This request based on the intent/action mapped needs you to trigger another external WS , lets say Twilio to send SMS.
  • Twilio sent the SMS to the user and returned success to your webhook that message ID is  e.g. "123458787887887888"
    • As this is technical information, you did not send this Message ID back to the AI platform --> FB (hence no response for the actual request http-req1)
  • So the initial request from AI platform "http-req1" did not get any response back via the reverse flow  i.e. your web-hook to AI platform --> User chat platform.
  • This request will keep on waiting on the same port until is gets a response.
  • Now this will clog your port for next requests in sometime and hence you will get the hung up error and a crash.
Now the solutions on web will make more sense to you.
e.g. If you will use req.end  or res.send to end such requests explicitly --> You know where to use it i.e. when you do not send response back for the actual request to the user.

We used req.end as the simplest solution for this problem for this specific use case.
In some cases where you generate the response with your server and getting this error, use res.end i.e. response.end.








Comments