Last active
January 8, 2023 10:11
-
-
Save OmarShehata/a67499b29c65c8b84f74760652890bd8 to your computer and use it in GitHub Desktop.
Socket.io simple cheat sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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