Skip to content

Instantly share code, notes, and snippets.

@OmarShehata OmarShehata/socket.js
Last active Dec 2, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.