Implementing a Basic Failsafe Socket

Learn how to use the State pattern to create a client socket that doesn’t break when it loses connection with the server.

We'll cover the following

Let’s now work on a more concrete example so that we can apply what we’ve learned about the State pattern.

Let’s build a TCP client socket that doesn’t fail when the connection with the server is lost; instead, we want to queue all the data sent during the time in which the server is offline and then try to send it again as soon as the connection is reestablished. We want to leverage this socket in the context of a simple monitoring system, where a set of machines sends some statistics about their resource utilization at regular intervals. If the server that collects these resources goes down, our socket will continue to queue the data locally until the server comes back online.

Let’s start by creating a new module called failsafeSocket.js that defines our context object.

Get hands-on with 1200+ tech skills courses.