Skip to content

Instantly share code, notes, and snippets.

@OmarShehata
Last active January 8, 2023 10:11
Show Gist options
  • Save OmarShehata/a67499b29c65c8b84f74760652890bd8 to your computer and use it in GitHub Desktop.
Save OmarShehata/a67499b29c65c8b84f74760652890bd8 to your computer and use it in GitHub Desktop.
Socket.io simple cheat sheet
io.on('connection', function(socket){
/* 'connection' is a special event fired on the server when any new connection is made */
})
socket.on('disconnect', function(){
/* When this individual socket has disconnected, this special event fires */
})
/* This will send the event 'foobar' with the data to
every connected to socket */
io.emit('foobar',data)
/* This will send the event 'foobar' with the data object to
every connected socket EXCEPT for the socket this is called on */
socket.broadcast.emit('foobar',data)
socket.on('foobar', function(data){
/* Fires whenever this socket receives an event 'foobar'.
You can access the data received inside this callback*/
})
/* This will send the event 'foobar' with the data to the server */
socket.emit('foobar',data)
socket.id // A unique string identifying this socket connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment