What do you want to save?
Add Code snippet
New code examples
-
Other 2022-02-02 20:06:15
how to check whether the user is online or not using socket.io
const users = {}; io.on('connection', function(socket){ console.log('a user connected'); socket.on('login', function(data){ console.log('a user ' + data.userId + ' connected'); // saving userId to array with socket ID users[socket.id] = da... Add solution -
Other 2021-11-22 04:02:13
play audio button
<script> var audio = new Audio("http://music.ogg" ) ; audio.oncanplaythrough = function(){ audio.play(); } audio.loop = true; audio.onended = function(){ audio.play(); } </script> <input type="image" src="http:... Add solution -
Javascript 2021-10-04 00:07:06
socket.io do sockets leave rooms on disconnect
/* Upon disconnection, sockets leave all the channels they were part of automatically, and no special teardown is needed on your part. You can fetch the rooms the Socket was in by listening to the disconnecting event: */ io.on('connection', socket =&g... Add solution -
Other 2021-09-13 21:42:02
socket.io emit cheatsheet
io.on('connect', onConnect);function onConnect(socket){ // sending to the client socket.emit('hello', 'can you hear me?', 1, 2, 'abc'); // sending to all clients except sender socket.broadcast.emit('broadcast', 'hello friends!'); // sending to all cl... Add solution -
Other 2021-09-10 03:18:02
rooms in socket io
io.on('connection', socket => { socket.join('some room'); }); //And then simply use to or in (they are the same) when broadcasting or emitting: io.to('some room').emit('some event'); //You can emit to several rooms at the same time: io.to('room1').... Add solution