3

There are some blog posts and questions written about this, but they are mostly from 2010 or 2011, and I was hoping for a more up-to-date (2014 era) answer.

I have a pretty standard Node.JS application running Express, Socket.IO, Passenger, etc. My production server is running Ubuntu Server 12.04 LTS. At a high level, my deployment workflow is roughly:

  1. Push/pull my updated code to my production server using git
  2. If necessary, run any build scripts and/or install new dependencies
  3. Restart the app using forever restart

The Node.JS app is running on port 8080, and I've got iptables set up to forward the port 80 traffic over to the Node application.

There are some problems with this setup. Most significant is the interruption to service. Since my application is using WebSockets, whenever forever restarts the server, all connected users lose their WebSockets session. Socket.IO automatically reconnects the users, but the users' session information doesn't carry over to the new server instance. Another issue is with error handling. If the Node.JS server encounters an unhanded exception (which is an unfortunate reality), forever kills and restarts the server, again causing all users to lose their WebSockets session.

Servers deployed with more traditional technologies like PHP and Apache don't have this problem because (if I understand it correctly) each connection runs in its own thread, so a new deployment doesn't affect the currently active users, and an exception in PHP disrupts only one user's session rather than killing the server for everyone.

So, how to deploying Node.JS without interrupting service so it can preserve session per user?

masegaloeh
  • 18,236
  • 10
  • 57
  • 106
sffc
  • 382
  • 1
  • 3
  • 11
  • 1
    Consider storing your websocket session data outside of the node process - e.g. using Redis (Socket.io has a Redis-adapter). By keeping the data independent of the Node.js process, you can run multiple Node.js processes (e.g. a cluster) that share the same data and can restart any of them without losing your data. A good starting point for this is the cluster module. – cyberx86 Dec 20 '14 at 00:46

0 Answers0