You can't have error handling middleware for socket.io as in express, but you can still do that a little bit differently, just by wrapping the callback in your own error handler, like:
const errorHandler = (handler) => { const handleError = (err) => { console.error("please handle me", err); }; return (...args) => { try { const ret = handler.apply(this, args); if (ret && typeof ret.catch === "function") { // async handler ret.catch(handleError); } } catch (e) { // sync handler handleError(e); } };};// server or client sidesocket.on("hello", errorHandler(() => { throw new Error("let's panic");}));
Example is taken from the official documentation