socket.io functions

Sever/Node.js 2019. 8. 9. 10:27
반응형

https://dev.to/moz5691/socketio-for-simple-chatting---1k8n


socket.emit('message', "this is a test"); //sending to sender-client only
socket.broadcast.emit('message', "this is a test"); //sending to all clients except sender
socket.broadcast.to('game').emit('message', 'nice game'); //sending to all clients in 'game' room(channel) except sender
socket.to('game').emit('message', 'enjoy the game'); //sending to sender client, only if they are in 'game' room(channel)
socket.broadcast.to(socketid).emit('message', 'for your eyes only'); //sending to individual socketid
io.emit('message', "this is a test"); //sending to all clients, include sender
io.in('game').emit('message', 'cool game'); //sending to all clients in 'game' room(channel), include sender
io.of('myNamespace').emit('message', 'gg'); //sending to all clients in namespace 'myNamespace', include sender
socket.emit(); //send to all connected clients
socket.broadcast.emit(); //send to all connected clients except the one that sent the message
socket.on(); //event listener, can be called on client to execute on server
io.sockets.socket(); //for emiting to specific clients
io.sockets.emit(); //send to all connected clients (same as socket.emit)
io.sockets.on() ; //initial connection from a client.

반응형

'Sever > Node.js' 카테고리의 다른 글

Node 다른 서버 API 호출  (0) 2020.06.11
node.js socket.io  (0) 2019.08.09
mmserver  (0) 2019.07.10
Node.js에서 mysql을 async/await으로 작성하기  (0) 2019.01.16
nodejs mysql RESTful API (yellobean-server-00)  (0) 2019.01.08
: