Reputation: 17
Server side code:
io.on('connection',(socket)=>{
console.log('New WEBSocket Connection')
socket.emit('message',"welcome")
socket.broadcast.emit('message',"A new user joined")
socket.on('textMessage',(message,callback)=>{
const filter = new Filter();
if(filter.isProfane(message)){
return callback('Mind your languezz')
}
io.emit('message',message)
callback()
})
}
Client side code:
socket.emit('textMessage',message,(error)=>{
if(error){
return console.log(error)
}
console.log("Message Delivered")
})
My doubt is, on client side code, what if I used io.on instead of socket.on?
socket.on('textMessage',(message,callback)=>{............
instead I did it like this:
io.on('textMessage',(message,callback)=>{.............
Upvotes: 0
Views: 722
Reputation: 20701
io.on
listens to the server events. connection
is an event on the server, when a socket is connected. And when that socket is connected, the callback function inside that is run.
socket.on
listens to events on that connected socket. socket.on('textMessage'
asks to do something when textMessage
event is emitted on the socket, which you do from your client using socket.emit('textMessage'
Upvotes: 2