I have researched this topic already, but i failed to get to a conclusion.
My goal is to when for example when insufficient parameters get passed to the server or when someone failed to authenticate etc. I can return an error to the client.
the two options I found were the following:
- In the first option you create a custom (or Socket.io’s error event, idk?) error event on socket level where you pass all errors to the client. The client always listens to this error event and will handle all errors in the same place.
- In the second option you make use of callbacks. When something goes wrong you receive the error in the place where you run the code
When you use callbacks I found out when you pass en Error object, because of serialization, some properties get lost. You can map the error to a custom object, or use the flag ‘volatile’. This does cause the error to not get saved by Socket.io in the internal buffer. I do not know if this is good or bad. The message will be delivered immediately, however you cannot retrieve the data later anymore this way.
Now I am curious what the most common way to handle errors in Socket.io is and what the advantages are.