Reputation:
Tried different methods, but the data is sent to a maximum of one or two clients. How to send data to all the clients connected to the server ? What am I doing wrong?
Server.js:
var PORT = 3000; var options = { // 'log level': 0 }; var express = require('express'); var app = express(); var http = require('http'); var server = http.createServer(app); var io = require('socket.io').listen(server, options); server.listen(PORT); app.get('/', function (req, res) { res.sendfile(__dirname + '/attantions/templates/.default/template.php'); }); io.sockets.on('connection', function (client) { client.on('attantion', function (data) { try { // Tried so io.sockets.volatile.emit('attantion', data); // And tried so io.sockets.emit('attantion', data); client.emit('attantion', data); client.broadcast.emit('attantion', data ); } catch (e) { console.log(e); client.disconnect(); } }); });
Client.js:
socket.emit("attantion", data); socket.on('attantion', function (data) { pushData(data); });
Upvotes: 1
Views: 1754
Reputation: 4985
See this post for different options for socket.io messages
Send response to all clients except sender (Socket.io)
io.sockets.on('connection', function (client) {
client.on('attantion', function (data) {
//client.emit('attantion', data ); // This will send it to only the client
//client.broadcast.emit('attantion', data); // This will send it to everyone but this client
io.emit('attantion', data); // This will send it to all attached sockets.
});
});
Edit
I wonder if this post can help you?
I was curious how sending the php file to the client through node.js works? are you using another framework?
Could you show more of what your client code looks like? loading the lib and the instantiation of the socket.
Upvotes: 0