socket.io-redis-adapter icon indicating copy to clipboard operation
socket.io-redis-adapter copied to clipboard

Error: read ECONNRESET at TCP.onStreamRead

Open mzahidriaz opened this issue 3 years ago • 0 comments

Getting following error "socket.io": "^4.4.1" "@socket.io/redis-adapter": "^7.1.0"

Apr 15 07:48:12 ip-172-31-30-80 web: REDIS ADAPTOR DISCONNECTED ON pubClient Error: read ECONNRESET
Apr 15 07:48:12 ip-172-31-30-80 web: at TCP.onStreamRead (internal/stream_base_commons.js:209:20) {
Apr 15 07:48:12 ip-172-31-30-80 web: errno: -104,
Apr 15 07:48:12 ip-172-31-30-80 web: code: 'ECONNRESET',
Apr 15 07:48:12 ip-172-31-30-80 web: syscall: 'read'
Apr 15 07:48:12 ip-172-31-30-80 web: }
Apr 15 07:48:12 ip-172-31-30-80 web: (node:425) UnhandledPromiseRejectionWarning: Error: read ECONNRESET
Apr 15 07:48:12 ip-172-31-30-80 web: at TCP.onStreamRead (internal/stream_base_commons.js:209:20)
Apr 15 07:48:12 ip-172-31-30-80 web: (Use `node --trace-warnings ...` to show where the warning was created)
Apr 15 07:48:12 ip-172-31-30-80 web: (node:425) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
Apr 15 07:48:12 ip-172-31-30-80 web: (node:425) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code

using Node 14 will not cause the server to crash on above, with node v16 server crashed with above error.

here is my code

import { IoAdapter } from '@nestjs/platform-socket.io'
import { createClient } from 'redis'
import { createAdapter } from '@socket.io/redis-adapter'
import { ServerOptions } from 'socket.io'

const redisPort = process.env.REDIS_PORT ? +process.env.REDIS_PORT : 6379
const redisHost = process.env.REDIS_HOST || 'localhost'

export class RedisIoAdapter extends IoAdapter {
	private adapterConstructor: ReturnType<typeof createAdapter>

	async connectToRedis(): Promise<void> {
		const pubClient = createClient({ url: `redis://${redisHost}:${redisPort}` })
		const subClient = pubClient.duplicate()

		pubClient.on('error', (err) => {
			console.error(`REDIS ADAPTOR DISCONNECTED ON pubClient`, err)
		})
		subClient.on('error', (err) => {
			console.error(`REDIS ADAPTOR DISCONNECTED ON subClient`, err)
		})

		await Promise.all([pubClient.connect(), subClient.connect()])

		this.adapterConstructor = createAdapter(pubClient, subClient, {
			publishOnSpecificResponseChannel: true,
			requestsTimeout: 10000,
		})
	}

	createIOServer(port: number, options?: ServerOptions): any {
		const opts: Partial<ServerOptions> = {
			pingInterval: 5000,
			pingTimeout: 2000,
			cors: {
				origin: ['http://localhost:3000'],
				methods: ['GET', 'POST'],
				credentials: true,
			},
		}
		const server = super.createIOServer(port, Object.assign(options, opts))
		server.of('/').adapter.on('error', (err) => {
			console.error('socket server error', err)
		})
		server.adapter(this.adapterConstructor)

		return server
	}
}

mzahidriaz avatar Apr 15 '22 07:04 mzahidriaz