queelag / fcm

MIT License
6 stars 2 forks source link

How to handle socketError and reconnect? #10

Open juangvega opened 1 month ago

juangvega commented 1 month ago

Hello,

Occasionally, I'm getting this error:

FcmClient -> onSocketError Error: read ECONNRESET at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) { errno: -104, code: 'ECONNRESET', syscall: 'read' } FcmClient -> onSocketClose -> The socket has been closed with errors. [ true ]

Is there a way I can handle and reconnect when such an error occurs? Thanks

alchemicas commented 1 month ago

Hey @juangvega , you can listen to the close event both from the FcmClient and its socket, for example:

import { FcmClient } from '@aracna/fcm'

const client = new FcmClient()

client.on('close', () => console.log('received close message from mcs'))
client.socket.on('close', () => console.log('connection to the socket closed'))

Then in the callbacks you can decide whether to reconnect or not using the connect method from the FcmClient instance, hope this helps!

juangvega commented 1 month ago

Hi @alchemicas , that definitely helped. Thank you.

Another question: Is there a way I can monitor the status of the FcmClient? Like, if it is actually listening? I have noticed that occasionally, everything seems to be running fine, but I'm not getting any messages. Then, I restart the service, and it starts to receive messages.

Thanks

alchemicas commented 1 month ago

Hey @juangvega , glad to hear that! To monitor the status you could try listening to the "heartbeat" event, or go lower level and monitor something like the "data" event of the socket.

You could also try increasing the heartbeat frequency, maybe you stop getting messages because I've put it too high, which might be another issue entirely, lmk if that changes things, thank you!