Socket.io event works randomly - node.js

What I'm trying to do is to emit a event based on the progress of my jobs. It's working in the Gateway(proxy) side, the logs appear, etc, but when I'm consuming the event on the front-end, sometimes it works, sometimes not, and it throws 'ping timeout' error in the console. If I restart the nodejs service a few times it works.
I'm open to ideas of alternative ways to implement this feature.
SocketController
export default class SocketController {
socket: any;
interval: any;
instance: any;
queue: any;
constructor(server) {
// Creating Websocket connection
this.instance = new Server(server, {
cors: { origin: process.env.FRONTEND_URL },
path: "/socket.io/"
});
this.socket = null;
this.queue = null;
this.instance.on("connection", (socket) => {
let connectedUsersCount =
Object.keys(this.instance.sockets.sockets).length + 1;
let oneUserLeft = connectedUsersCount - 1;
console.log("New client connected ", connectedUsersCount);
// Assign socket to the class
this.socket = this.socket == null ? socket : this.socket;
/*
if (this.interval) {
clearInterval(this.interval);
}
*/
// initialize Queue
this.queue = this.queue === null ? new QueueService(socket) : this.queue;
socket.on("disconnect", () => {
console.log("Client disconnected ", oneUserLeft);
// clearInterval(this.interval);
});
});
}
QueueService
export default class QueueService {
channels: any;
socket: any;
constructor(socket: any) {
this.channels = ["integrationProgress", "news"];
this.socket = socket;
integrationQueueEvents.on("progress", (job: any) => {
console.log("Job Progressing", job);
this.socket.emit("integrationProgress", { status: true, data: job.data })
});
integrationQueueEvents.on("active", ({ jobId }) => {
console.log(`Job ${jobId} is now active`);
});
integrationQueueEvents.on("completed", ({ jobId, returnvalue }) => {
console.log(`${jobId} has completed and returned ${returnvalue}`);
this.socket.emit("integrationComplete", {
status: true,
message: returnvalue
});
});
integrationQueueEvents.on("failed", ({ jobId, failedReason }) => {
console.log(`${jobId} has failed with reason ${failedReason}`);
this.socket.emit("integrationProgress", {
status: false,
message: failedReason
});
});
}
}
Front-End
const socket = io(process.env.GATEWAY_URL, {
path: "/socket.io/"
});
socket.on("connect_error", (err) => {
console.log(`connect_error due to ${err.message}`);
socket.connect();
});
socket.on("disconnect", (socket) => {
console.log(socket);
console.log("Client disconnected ");
});
socket.on("connect", (socket) => {
console.log("Client Connected ");
console.log(socket);
});
socket.on("integrationProgress", async (socket) => {
try {
console.log(`Progress: ${socket.data}`);
updateJob(socket.data);
} catch (err) {
console.log(err);
}
});

Related

Jest websocket test is throwing an error "thrown: "Exceeded timeout of 5000 ms for a test"

I am using the ws library to create a websocket server
I am getting this error on running the test
src/server/server.ts
import { Server } from 'http';
import WebSocket from 'ws';
import { sessionHandler } from './sessionHandler';
export const websocketServer = new WebSocket.Server({
noServer: true,
clientTracking: false,
path: '/ws',
});
const WEBSOCKET_CHECK_IDLE_CONNECTION_FREQUENCY =
+process.env.WEBSOCKET_CHECK_IDLE_CONNECTION_FREQUENCY;
function checkIfConnectionIsAlive(ws: WebSocket) {
if (ws.isAlive === false) {
clearTimeout(ws.timer);
return ws.terminate();
}
ws.isAlive = false;
ws.ping();
ws.timer = setTimeout(
checkIfConnectionIsAlive,
WEBSOCKET_CHECK_IDLE_CONNECTION_FREQUENCY,
ws,
);
}
function heartbeat() {
this.isAlive = true;
}
export function upgradeToWebSocketConnection(server: Server) {
server.on('upgrade', (request, socket, head) => {
sessionHandler(request, {}, () => {
websocketServer.handleUpgrade(request, socket, head, (ws) => {
websocketServer.emit('connection', ws, request);
});
});
});
}
websocketServer.on('connection', (ws, request) => {
ws.isAlive = true;
ws.on('pong', heartbeat);
ws.send('hello');
checkIfConnectionIsAlive(ws);
});
websocketServer.on('close', () => {
console.debug('Shutting down websocket server');
});
I wrote a simple test to check if I get that hello message on connecting to the server
src/tests/websocket.test.ts
import http, { Server } from 'http';
import {
upgradeToWebSocketConnection,
websocketServer,
} from 'server/websocket';
import WebSocket from 'ws';
function startServer(port: number): Promise<Server> {
const server = http.createServer();
return new Promise((resolve) => {
server.listen(port, () => resolve(server));
});
}
function waitForSocketState(socket: WebSocket, state: number): Promise<void> {
return new Promise(function (resolve) {
setTimeout(function () {
if (socket.readyState === state) {
resolve();
} else {
waitForSocketState(socket, state).then(resolve);
}
});
});
}
let server: Server;
describe('Websocket server tests', () => {
beforeEach(async () => {
server = await startServer(8000);
upgradeToWebSocketConnection(server);
});
afterEach(() => {
websocketServer.emit('close');
server.close();
});
test('Server sends hello to the client on connect', async () => {
const client = new WebSocket(`ws://localhost:8000/ws`);
let message: WebSocket.Data;
client.on('message', (data) => {
message = data.toString();
client.close();
});
await waitForSocketState(client, client.OPEN);
await waitForSocketState(client, client.CLOSED);
expect(message).toBe('hello');
});
});
Can someone kindly tell me how to fix this error and make the test succeed? My guess is that it has a problem with the setTimeout section

How to establish TCP hole punching with node.js?

I have been trying to establish TCP hole punching with Node.js, and I am not sure if it fails because of my NAT or because the code is erroneous.
The following code intends to:
let 2 clients register on a server the 4-tuple (address on client, port on client, address as seen by server, port as seen by server)
let the server signal when the 2 clients are mutually ready by sending them each other's 4-tuple (tryConnectToPeer)
let each client start a local server (listen) on the local address and port used when communicating with the server (address on client, port on client)
when the local server is running, try to establish a connection (connect) with the local port & address of the other client, as well as an external connection with the port & address of the other client, as the server was seeing them (probably the other client's router address and port then)
Client code - I would imagine the mistake is here:
import { createConnection, createServer } from 'net';
const serverPort = 9999;
const serverHost = '192.168.1.19'
const socket = createConnection(serverPort, serverHost);
socket.setEncoding('utf8');
socket.on('data', (data: string) => {
console.log('data', data);
let parsedData: any = null;
try {
parsedData = JSON.parse(data);
} catch (e) {
if (e instanceof Error) {
console.log(e.message);
} else {
throw e;
}
}
if (parsedData?.command === 'tryConnectToPeer') {
console.log('Will try to connect with peer:', parsedData);
const server = createServer(c => {
console.log('client connected');
c.setEncoding('utf8');
c.on('data', (data: string) => {
console.log('received:', data);
c.write('hi!');
});
});
server.listen(socket.localPort, socket.localAddress, () => {
console.log('server bound to ', socket.localAddress, socket.localPort);
});
server.on('listening', () => {
console.log('Attempting local connection', parsedData.localAddress, parsedData.localPort);
const localSocket = createConnection({ port: parsedData.localPort, host: parsedData.localAddress });
localSocket.on('error', (e) => {
console.error('Failed to connect with peer locally');
console.error(e);
});
localSocket.setEncoding('utf8');
localSocket.on('data', (data: string) => {
console.log(data);
localSocket.write('ho! on local')
})
console.log('Attempting external connection', parsedData.externalAddress, parsedData.externalPort);
const externalSocket = createConnection({ port: parsedData.externalPort, host: parsedData.externalAddress});
externalSocket.on('error', (e) => {
console.error('Failed to connect with peer externally');
console.error(e);
});
externalSocket.setEncoding('utf8');
externalSocket.on('data', (data: string) => {
console.log(data);
externalSocket.write('ho! on external')
})
localSocket.on('connect', () => {
externalSocket.end();
localSocket.write('start from localsocket');
console.log('connected to peer locally!');
})
externalSocket.on('connect', () => {
// localSocket.end();
externalSocket.write('start from externalSocket');
console.log('connected to peer externally!');
})
})
}
});
socket.on('connect', () => {
socket.write(JSON.stringify(
{
command: 'register',
localPort: socket.localPort,
localAddress: socket.localAddress
}
));
});
Server code - a tad long, but probably not the problematic piece:
import { createServer, Socket } from 'net';
type AddressAndPort = {
address: string | undefined,
port: number | undefined
}
class ConnectionDescriptor {
socket: Socket;
addressAndPortOnClient: AddressAndPort;
addressAndPortSeenByServer: AddressAndPort;
constructor({ socket, addressAndPortOnClient, addressAndPortSeenByServer } : {socket: Socket, addressAndPortOnClient: AddressAndPort, addressAndPortSeenByServer: AddressAndPort}) {
this.socket = socket;
this.addressAndPortOnClient = addressAndPortOnClient;
this.addressAndPortSeenByServer = addressAndPortSeenByServer;
}
toString() {
return JSON.stringify({
addressAndPortOnClient: this.addressAndPortOnClient,
addressAndPortSeenByServer: this.addressAndPortSeenByServer
});
}
}
class ConnectionDescriptorSet {
connectionDescriptors: ConnectionDescriptor[]
constructor() {
this.connectionDescriptors = [];
}
get full() {
return this.connectionDescriptors.length === 2;
}
add(descriptor: ConnectionDescriptor) {
if (!descriptor.addressAndPortOnClient.address || !descriptor.addressAndPortOnClient.port || !descriptor.addressAndPortSeenByServer.address || !descriptor.addressAndPortSeenByServer.port) {
throw new Error(`Cannot register incomplete connection descriptor: ${JSON.stringify(descriptor)}`);
}
const index = this.connectionDescriptors.findIndex(c => c.addressAndPortSeenByServer.address === descriptor.addressAndPortSeenByServer.address && c.addressAndPortSeenByServer.port === descriptor.addressAndPortSeenByServer.port);
if (index === -1) {
console.log('Registering new client:');
console.log(descriptor.toString());
if (this.connectionDescriptors.length === 2) {
throw new Error('Only two clients can be registered at a time!');
}
this.connectionDescriptors.push(descriptor)
} else {
console.log('Client already registered:');
console.log(descriptor.toString());
}
}
remove(addressAndPortSeenByServer: AddressAndPort) {
const index = this.connectionDescriptors.findIndex(c => c.addressAndPortSeenByServer.address === addressAndPortSeenByServer.address && c.addressAndPortSeenByServer.port === addressAndPortSeenByServer.port);
if (index === -1) {
console.log('Client with following connectionDescriptors was not found for removal:');
console.log(JSON.stringify(addressAndPortSeenByServer));
} else if (index === 0) {
console.log('Removing client:');
console.log(this.connectionDescriptors[0].toString());
this.connectionDescriptors.shift();
} else if (index === 1) {
console.log('Removing client:');
console.log(this.connectionDescriptors[1].toString());
this.connectionDescriptors.pop();
} else {
throw new Error('No more than 2 clients should have been registered.');
}
}
}
const connectionDescriptorSet = new ConnectionDescriptorSet();
const server = createServer((c) => {
console.log('client connected');
// Optional - useful when logging data
c.setEncoding('utf8');
c.on('end', () => {
connectionDescriptorSet.remove({ address: c.remoteAddress, port: c.remotePort })
console.log('client disconnected');
});
c.on('data', (data: string) => {
console.log('I received:', data);
try {
const parsedData = JSON.parse(data);
if (parsedData.command === 'register') {
connectionDescriptorSet.add(new ConnectionDescriptor({
socket: c,
addressAndPortOnClient: {
address: parsedData.localAddress,
port: parsedData.localPort
},
addressAndPortSeenByServer: {
address: c.remoteAddress,
port: c.remotePort
}
}));
if (connectionDescriptorSet.full) {
console.log('connectionDescriptorSet full, broadcasting tryConnectToPeer command');
connectionDescriptorSet.connectionDescriptors[0].socket.write(
JSON.stringify({
command: 'tryConnectToPeer',
localPort: connectionDescriptorSet.connectionDescriptors[1].addressAndPortOnClient.port,
localAddress: connectionDescriptorSet.connectionDescriptors[1].addressAndPortOnClient.address,
externalAddress: connectionDescriptorSet.connectionDescriptors[1].addressAndPortSeenByServer.address,
externalPort: connectionDescriptorSet.connectionDescriptors[1].addressAndPortSeenByServer.port,
})
);
connectionDescriptorSet.connectionDescriptors[1].socket.write(
JSON.stringify({
command: 'tryConnectToPeer',
localPort: connectionDescriptorSet.connectionDescriptors[0].addressAndPortOnClient.port,
localAddress: connectionDescriptorSet.connectionDescriptors[0].addressAndPortOnClient.address,
externalAddress: connectionDescriptorSet.connectionDescriptors[0].addressAndPortSeenByServer.address,
externalPort: connectionDescriptorSet.connectionDescriptors[0].addressAndPortSeenByServer.port,
})
);
}
}
} catch (e) {
if (e instanceof Error) {
console.error(e);
c.write(e.message);
} else {
throw e;
}
}
})
});
server.on('error', (err) => {
throw err;
});
server.listen(9999, () => {
console.log('server bound');
});
This code works when the two clients are on the same local network, but fails when they are on different networks.
There is in fact no need to run a server on each client once they got each other's credentials. Having a server may increase the chances of EADDRINUSE - unsure.
A simple connect to each other suffices. First one to try will fail, second may succeed.
It's important to specify the origination port of the socket making the call to the other peer (both for the private and public calls). I believe this was the issue in the above code.
Network topology is also tricky. It's best to have the server and each client each behind a different NAT.
Working proof of concept can be found here:
https://github.com/qbalin/tcp_hole_punching_node_js/tree/main

Socket.io and React: Warning: Can't perform a React state update on an unmounted component

I am a relative newcomer to React and implementing a chat app with react, socket.io, and node.
When the user is chatting with someone, he would connect to a socket. It works fine, but if the user leaves to another page, and RE-ENTERS the same chat again, a second socket is connected(bad), and I get the "Can't perform a React state update on an unmounted component" error.
I did implement code to have socket leave the room (on the second useEffect), if the user leaves the page. But that doesn't seem to work.
Thanks in advance,
Error:
Partial React frontend code:
useEffect(() => {
socket.emit("enter chatroom", { conversationId: props.chatId });
setChatId(props.chatId);
getMessagesInConversation(props.chatId, (err: Error, result: any) => {
if (err) {
setCerror(err.message);
} else {
setMessages(buildMessages(result.messages).reverse());
}
});
socket.on("received", (data: any) => {
setMessages((messages: any) => {
console.log("messages");
console.log(messages);
return [...messages.slice(-4), ...buildMessages(data.newMsg)];
});
});
}, []);
useEffect(() => {
return () => {
socket.emit("leaveChatroom", {
conversationId: chatId,
});
};
}, [chatId]);
simplified nodejs code
private ioMessage = () => {
this._io.on("connection", (socket) => {
console.log("socket io connected");
const socketUser = socket.request.user;
socket.on("enter chatroom", (data) => {
const room_status = Object.keys(socket.rooms).includes(
data.conversationId
);
if (!room_status) { // should only join if socket is not already joined.
socket.join(data.conversationId);
}
});
socket.on("leaveChatroom", (data) => {
socket.leave(data.conversationId);
});
socket.on("chat message", async (msg) => {
const database = getDB();
const newMessage = {
...msg,
createdAt: Date.now(),
};
await database.collection("message").insertOne(newMessage);
const messages = await database
.collection("message")
.find({ conversationId: msg.conversationId })
.toArray();
this._io.to(msg.conversationId).emit("received", {
newMsg: [messages[messages.length - 1]],
});
});
socket.on("disconnect", (socket) => {
delete this._users[socketUser.userId];
console.log("--- disconnect : ", socketUser);
console.log("--- active users: ", this._users);
});
});
};
detailed error log:

NodeJS generic-pool how to set request timeout?

I'm new to using the generic-pool. I see that there is a maxWaitingClients setting, but it doesn't specify for how long a request can stay in the queue before it times out. Is there any way for me to specify such a timeout setting?
EDIT: Added my code for how I'm currently using generic-pool:
function createPool() {
const factory = {
create: function() {
return new Promise((resolve, reject) => {
const socket = new net.Socket();
socket.connect({
host: sdkAddress,
port: sdkPort,
});
socket.setKeepAlive(true);
socket.on('connect', () => {
resolve(socket);
});
socket.on('error', error => {
reject(error);
});
socket.on('close', hadError => {
console.log(`socket closed: ${hadError}`);
});
});
},
destroy: function(socket) {
return new Promise((resolve) => {
socket.destroy();
resolve();
});
},
validate: function (socket) {
return new Promise((resolve) => {
if (socket.destroyed || !socket.readable || !socket.writable) {
return resolve(false);
} else {
return resolve(true);
}
});
}
};
return genericPool.createPool(factory, {
max: poolMax,
min: poolMin,
maxWaitingClients: poolQueue,
testOnBorrow: true
});
}
const pool = createPool();
async function processPendingBlocks(ProcessingMap, channelid, configPath) {
setTimeout(async () => {
let nextBlockNumber = fs.readFileSync(configPath, "utf8");
let processBlock;
do {
processBlock = ProcessingMap.get(channelid, nextBlockNumber);
if (processBlock == undefined) {
break;
}
try {
const sock = await pool.acquire();
await blockProcessing.processBlockEvent(channelid, processBlock, sock, configPath, folderLog);
await pool.release(sock);
} catch (error) {
console.error(`Failed to process block: ${error}`);
}
ProcessingMap.remove(channelid, nextBlockNumber);
fs.writeFileSync(configPath, parseInt(nextBlockNumber, 10) + 1);
nextBlockNumber = fs.readFileSync(configPath, "utf8");
} while (true);
processPendingBlocks(ProcessingMap, channelid, configPath);
}, blockProcessInterval)
}
Since you are using pool.acquire(), you would use the option acquireTimeoutMillis to set a timeout for how long .acquire() will wait for an available resource from the pool before timing out.
You would presumably add that option here:
return genericPool.createPool(factory, {
max: poolMax,
min: poolMin,
maxWaitingClients: poolQueue,
testOnBorrow: true,
acquireTimeoutMillis, 3000 // max time to wait for an available resource
});

Can not emit event with socketid

I am trying to write an integration test for socket io. I am using try-catch for server event when the error is catched I emit an event for the client to handle the error
io.of('/rooms').on('connection', socket => {
const socketId = socket.id;
console.log('server socketid', socketId);
const userRepository = getCustomRepository(UserRepository);
const conversationToUserRepository = getCustomRepository(ConversationToUserRepository);
socket.on('initGroupChat', async (users_id, fn) => {
try {
const [user, listConversationToUser] = await Promise.all([
userRepository.findOne({
where: {
users_id,
},
}),
conversationToUserRepository.find({
where: {
users_id,
},
}),
]);
if (!user) {
throw new NotFoundError('User not found with id: ' + users_id);
}
console.log(2);
user.socket_id = socket.id;
await userRepository.save(user);
for (const item of listConversationToUser) {
socket.join(item.conversation_id.toString());
}
fn('init group chat success');
} catch (error) {
io.to(socketId).emit('error', errorHandlerForSocket(error));
console.log(10);
}
});
});
but on the socket client, nothing happens. here is the code socket client:
it.only('Init Group Chat with error', done => {
socket = io.connect(`http://localhost:${env.app.port}/rooms`, {
transports: ['websocket']
});
const id = 11111;
socket.emit('initGroupChat', id, (response: any) => {
console.log('response', response);
});
socket.on('error', (error: any) => {
console.log('error3', error);
done();
});
});
on the error event, the console.log did not show on the terminate. it didn't catch the event I emit on the server.
can anyone help me fix this issue
Every time the client Refresh socketId changes.
Server :
io.emit("send_to_client", {
userId: 112233,
data: "Hello user 112233"
});
Client :
var userLogin = 112233;
socket.on("send_to_client", function(res) {
if(res.userId === userLogin)
//somethingElse
});

Resources