├── .gitignore ├── README.md ├── examples └── client.js ├── package.json ├── src └── index.js └── test └── reconnect.test.js /.gitignore: -------------------------------------------------------------------------------- 1 | .vimrc 2 | ~.swp 3 | ~ 4 | node_modules 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Reconnection Handler for TCP Socket Connections in node.js 2 | 3 | This module will help you reconnect a TCP socket connection if the connection is lost. 4 | 5 | When you want to stay connected with a TCP server you need to implement a reconnect logic. This module will keep your connection online if possible. For example, if you want to connect to a service that's not online yet, or when someone pulls the plug. 6 | 7 | # Example 8 | 9 | ``` 10 | let socket = net require('net').Socket() 11 | let options = { 12 | 'host' : 'somehost', 13 | 'port' : someport, 14 | 'retryTime' : 1000, // 1s for every retry 15 | 'retryAlways' : true // retry even if the connection was closed on purpose 16 | } 17 | let Reconnect = require('node-net-reconnect') 18 | let recon = new Reconnect(socket, options) 19 | 20 | socket.connect(options) 21 | 22 | socket.on('connect', function () { 23 | 24 | /* if you enabled retryAlways, a call to 25 | socket.end() will reconnect the client. 26 | In that case you need to close the connection 27 | through the recon.end() method. */ 28 | setTimeout(function () { 29 | recon.end() 30 | }, 10000) 31 | 32 | }) 33 | ```` 34 | 35 | # LICENCE 36 | 37 | Copyright (C) 2017 Stefan Poeter (Stefan.Poeter[at]cloud-automation.de) 38 | 39 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 40 | 41 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 42 | 43 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 44 | 45 | 46 | -------------------------------------------------------------------------------- /examples/client.js: -------------------------------------------------------------------------------- 1 | let Reconnect = require('../src/index.js') 2 | let net = require('net') 3 | let socket = new net.Socket() 4 | let options = { 'host': process.argv[2], 'port': process.argv[3], 'retryAlways': true } 5 | let NetKeepAlive = require('net-keepalive') 6 | 7 | Reconnect.apply(socket, options) 8 | 9 | socket.setKeepAlive(true, 1000) 10 | socket.setTimeout(1000) 11 | 12 | socket.connect(options) 13 | 14 | let timeout 15 | socket.on('connect', function () { 16 | NetKeepAlive.setKeepAliveInterval(socket, 1000) 17 | NetKeepAlive.setKeepAliveProbes(socket, 1) 18 | 19 | console.log('Unplug the network connection') 20 | }) 21 | 22 | socket.on('close', function () { 23 | clearTimeout(timeout) 24 | }) 25 | 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-net-reconnect", 3 | "version": "0.2.1", 4 | "description": "Reconnect logic for TCP sockets.", 5 | "main": "src/index.js", 6 | "scripts": { 7 | "test": "standard && mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/cloud-automation/node-net-reconnect.git" 12 | }, 13 | "keywords": [ 14 | "net", 15 | "tcp", 16 | "socket", 17 | "reconnect" 18 | ], 19 | "author": "Stefan Poeter ", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/cloud-automation/node-net-reconnect/issues" 23 | }, 24 | "homepage": "https://github.com/cloud-automation/node-net-reconnect#readme", 25 | "dependencies": { 26 | "debug": "^2.6.4", 27 | "net-keepalive": "^0.4.1" 28 | }, 29 | "devDependencies": { 30 | "mocha": "^3.3.0", 31 | "sinon": "^2.1.0" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | let debug = require('debug')('net-reconnect') 2 | let NetKeepAlive = require('net-keepalive') 3 | 4 | class NetReconnect { 5 | 6 | constructor (socket, options) { 7 | this._socket = socket 8 | this._options = options 9 | this._retryTime = options.retryTime || 1000 10 | this._retryAlways = options.retryAlways || false 11 | this._keepAliveDelay = Math.max(options.keepAliveDelay, 1000) || 1000 12 | this._keepAliveInterval = Math.max(options.keepAliveInterval, 1000) || 1000 13 | this._keepAliveProbes = Math.max(Math.min(options.keepAliveProbes, 1), 20) || 1 14 | this._closing = false 15 | 16 | this._socket.on('connect', this._onConnect.bind(this)) 17 | this._socket.on('close', this._onClose.bind(this)) 18 | this._socket.on('error', this._onError.bind(this)) 19 | } 20 | 21 | static apply (socket, options) { 22 | return new NetReconnect(socket, options) 23 | } 24 | 25 | _reconnect () { 26 | debug('reconnecting in %d', this._retryTime) 27 | setTimeout(function () { 28 | this._socket.connect(this._options) 29 | }.bind(this), this._retryTime) 30 | } 31 | 32 | _onConnect () { 33 | if (this._closing) { 34 | return 35 | } 36 | 37 | this._socket.setKeepAlive(true, this._keepAliveDelay) 38 | NetKeepAlive.setKeepAliveInterval(this._socket, this._keepAliveInterval) 39 | NetKeepAlive.setKeepAliveProbes(this._socket, this._keepAliveProbes) 40 | 41 | debug('online') 42 | } 43 | 44 | _onClose (hadError) { 45 | if (this._closing) { 46 | return 47 | } 48 | debug('offline') 49 | this._state = 'offline' 50 | if (!hadError) { 51 | debug('connection closed on purpose') 52 | if (this._retryAlways) { 53 | debug('retryAlways flag active, reconnecting') 54 | this._reconnect() 55 | return 56 | } else { 57 | debug('not reconnecting') 58 | return 59 | } 60 | } 61 | 62 | debug('connection closed with errors, reconnecting') 63 | 64 | this._reconnect() 65 | } 66 | 67 | _onError () { 68 | if (this._closing) { 69 | return 70 | } 71 | 72 | debug('error') 73 | } 74 | 75 | end () { 76 | debug('closing socket permanently') 77 | this._closing = true 78 | this._socket.removeListener('connect', this._onConnect) 79 | this._socket.removeListener('close', this._onClose) 80 | this._socket.removeListener('error', this._onError) 81 | return this._socket.end.apply(this._socket, arguments) 82 | } 83 | 84 | } 85 | 86 | module.exports = NetReconnect 87 | -------------------------------------------------------------------------------- /test/reconnect.test.js: -------------------------------------------------------------------------------- 1 | /* global describe, it, beforeEach */ 2 | 3 | let EventEmitter = require('events') 4 | let sinon = require('sinon') 5 | let Reconnect = require('../src/index.js') 6 | 7 | describe('NetReconnect Tests', function () { 8 | let socket 9 | 10 | beforeEach(function () { 11 | socket = new EventEmitter() 12 | socket.connect = function () { } 13 | socket.end = function () { } 14 | }) 15 | 16 | it('should reconnect on error', function (done) { 17 | let socketMock = sinon.mock(socket) 18 | let options = { } 19 | 20 | Reconnect.apply(socket, options) 21 | 22 | socketMock.expects('connect').withArgs(options).once() 23 | 24 | socket.emit('close', true) 25 | 26 | setTimeout(function () { 27 | socketMock.verify() 28 | done() 29 | }, 1100) 30 | }) 31 | it('should not reconnect when no error', function (done) { 32 | let socketMock = sinon.mock(socket) 33 | let options = { } 34 | 35 | Reconnect.apply(socket, options) 36 | 37 | socketMock.expects('connect').withArgs(options).never() 38 | 39 | socket.emit('close', false) 40 | 41 | setTimeout(function () { 42 | socketMock.verify() 43 | done() 44 | }, 1100) 45 | }) 46 | it('should reconnect when no error', function (done) { 47 | let socketMock = sinon.mock(socket) 48 | let options = { 'retryAlways': true } 49 | 50 | Reconnect.apply(socket, options) 51 | 52 | socketMock.expects('connect').withArgs(options).once() 53 | 54 | socket.emit('close', false) 55 | 56 | setTimeout(function () { 57 | socketMock.verify() 58 | done() 59 | }, 1100) 60 | }) 61 | it('should end connection', function (done) { 62 | let socketMock = sinon.mock(socket) 63 | let options = { 'retryAlways': true } 64 | 65 | let recon = new Reconnect(socket, options) 66 | 67 | socketMock.expects('connect').withArgs(options).never() 68 | 69 | recon.end() 70 | socket.emit('close', false) 71 | 72 | setTimeout(function () { 73 | socketMock.verify() 74 | done() 75 | }, 1100) 76 | }) 77 | }) 78 | --------------------------------------------------------------------------------