最終更新:2020-05-21 (木) 14:30:04 (7d)  

Socket.IO/チートシート
Top / Socket.IO / チートシート

Socket.IO/送信

  • https://socket.io/docs/emit-cheatsheet/
    socket.emit('hello', 'can you hear me?', 1, 2, 'abc')sending to the client
    socket.broadcast.emit('broadcast', 'hello friends!')sending to all clients except sender
    socket.to('game').emit('nice game', "let's play a game")sending to all clients in 'game' room except sender
    socket.to('game1').to('game2').emit('nice game', "let's play a game (too)")sending to all clients in 'game1' and/or in 'game2' room, except sender
    io.in('game').emit('big-announcement', 'the game will start soon')sending to all clients in 'game' room, including sender
    io.of('myNamespace').emit('bigger-announcement', 'the tournament will start soon')sending to all clients in namespace 'myNamespace', including sender
    io.of('myNamespace').to('room').emit('event', 'message')sending to a specific room in a specific namespace, including sender
    io.to?(socketId).emit('hey', 'I just met you')sending to individual socketid (private message)
    socket.to(socket.id).emit()WARNING: will NOT work, as it will send to everyone in the room named `socket.id` but the sender. Please use the classic `socket.emit()` instead.
    socket.emit('question', 'do you think so?', function (answer) {})sending with acknowledgement
    socket.compress?(false).emit('uncompressed', "that's rough")sending without compression
    socket.volatile.emit('maybe', 'do you really need it?')sending a message that might be dropped if the client is not ready to receive messages
    socket.binary?(false).emit('what', 'I have no binaries!')specifying whether the data to send has binary data
    io.local?.emit('hi', 'my lovely babies')sending to all clients on this node (when using multiple nodes)
    io.emit('an event sent to all connected clients')sending to all connected clients

メモ