Cannot read property 'EVENT' of undefined at Namespace.emit

102 Views Asked by At

I'd upgraded my socket.io version from 2 to 3 but the below line:

io.sockets.in(room).emit(bag.message.eventType, message)

breaks with this error:

Cannot read property 'EVENT' of undefined at Namespace.emit (/node_modules/socket.io/dist/namespace.js:170:49)

Can someone explain what syntax should go with socket.io@3 for the above code? TIA

1

There are 1 best solutions below

0
mj.akbarian On

As mentioned in this page https://socket.io/docs/v3/emit-cheatsheet/ you should change your code to this

io.in(room).emit(bag.message.eventType, message)

or

io.sockets.to(room).emit(bag.message.eventType, message)