io.in socket io code example
Example 1: js socket.emit
const socket = io('ws://localhost:3000');
socket.on('connect', () => {
socket.send('Hello!');
socket.emit('salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from([1, 2, 3, 4]));});
socket.on('message', data => {
console.log(data);
});
socket.on('greetings', (elem1, elem2, elem3) => {
console.log(elem1, elem2, elem3);
});
Example 2: socket emit to
io.to(socketid).emit('message', 'whatever');
Example 3: socket emit to specific room using nodejs socket.io
io.on('connection', socket => { socket.on('disconnecting', () => { const rooms = Object.keys(socket.rooms);
Example 4: socket.io client send data node js server
var socket = io.connect('http://localhost');
socket.emit('my other event', { my: 'data' });
io.sockets.on('connection', function (socket) {
socket.on('my other event', function (data) {
console.log(data);
});
});
socket.on("test", function (data) {
data.forEach(obj => {
console.log("Yer : " + obj.yer + ", Lat : " + obj.lat + ", Long : " + obj.lng);
})
});