jemz
jemz

Reputation: 5123

How to use events in socket.io?

I am newbie to socket.io,and I was looking for the events like "message" and I want to know what are they aside from message.

socket.on('message', function (msg) {

});

Thank you in advance.

Upvotes: 0

Views: 39

Answers (1)

agconti
agconti

Reputation: 18093

You create your own events; they are not predefined, ie:

socket.on('< whatever you want >', function (payload) {
   console.log('Hello World')
});

socket.emit('< whatever you want >', payload);

>>> 'Hello World'

For example:

socket.on('marco', function (payload) {
   console.log(payload.msg)
});

socket.emit('marco', {msg: 'polo!'});

>>> 'polo!'

There are some special names that are restricted however. These are:

  • 'error'
  • 'connect'
  • 'disconnect'
  • 'newListener'
  • 'removeListener

Upvotes: 2

Related Questions