Reputation:
What does socket.on and socket.emit mean? I can't seem to find explanations on the socket.io website.
Upvotes: 1
Views: 83
Reputation: 1602
socket.on
Listen to the socket on a specific keyword. Similar to JQuery on
socket.emit
"sends" a message to that keyword
The following code sends a message to "keyword"
socket.emit('keyword', 'Hello!');
The following code listens for messages on "keyword"
socket.on('keyword', function(res){
console.log(res)//prints "Hello!"
socket.emit('keyword', 'Got your message')
})
keyword can be any string you want. And both code snippets can be used client and server side.
Upvotes: 1