I'm working with socket.io and node.js . i am able to broadcast messages from server to all clients but i am facing problem while sending the message from server to specific clients. i am new to this socket.io and node.js
below is code snipped for clients and serve
server code :
var http = require('http'),
fs = require('fs');
var express = require('express');
var app = http.createServer(function (request, response) {
fs.readFile("client.html", 'utf-8', function (error, data) {
if(error)
{
responce.writeHead(404);
responce.write("File does not exist");
responce.end();
}
else
{
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
}
});
}).listen(1337);
var io = require('socket.io').listen(app);
var clients = [ ] ;
var socketsOfClients = {};
io.sockets.on('connection', function(socket)
{
socket.on('message_to_server', function(data)
{
clients.push(socket.id);
socket(clients[0]).emit("message_to_client" , { message: data["message"] });
});
});
~
clients code :
<!DOCTYPE html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
// our socket.io code goes here
var socketio = io.connect("127.0.0.1:1337");
socketio.on("message_to_client", function(data) {
document.getElementById("chatlog").innerHTML = ("<hr/>" +
data['message'] + document.getElementById("chatlog").innerHTML);
});
function sendMessage() {
var msg = document.getElementById("message_input").value;
socketio.emit("message_to_server", { message : msg});
}
</script>
</head>
<body>
<input type="text" id="message_input"/>
<button onclick="sendMessage()">send</button>
<div id="chatlog"></div>
</body>
</html>
~
when i am executing ,it is giving error like :
socket(clients[0]).emit("message_to_client" , { message: data["message"] });
^
TypeError: object is not a function at Socket.
I'm working with socket.io and node.js . i am able to broadcast messages from server to all clients but i am facing problem while sending the message from server to specific clients. i am new to this socket.io and node.js
below is code snipped for clients and serve
server code :
var http = require('http'),
fs = require('fs');
var express = require('express');
var app = http.createServer(function (request, response) {
fs.readFile("client.html", 'utf-8', function (error, data) {
if(error)
{
responce.writeHead(404);
responce.write("File does not exist");
responce.end();
}
else
{
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
}
});
}).listen(1337);
var io = require('socket.io').listen(app);
var clients = [ ] ;
var socketsOfClients = {};
io.sockets.on('connection', function(socket)
{
socket.on('message_to_server', function(data)
{
clients.push(socket.id);
socket(clients[0]).emit("message_to_client" , { message: data["message"] });
});
});
~
clients code :
<!DOCTYPE html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
// our socket.io code goes here
var socketio = io.connect("127.0.0.1:1337");
socketio.on("message_to_client", function(data) {
document.getElementById("chatlog").innerHTML = ("<hr/>" +
data['message'] + document.getElementById("chatlog").innerHTML);
});
function sendMessage() {
var msg = document.getElementById("message_input").value;
socketio.emit("message_to_server", { message : msg});
}
</script>
</head>
<body>
<input type="text" id="message_input"/>
<button onclick="sendMessage()">send</button>
<div id="chatlog"></div>
</body>
</html>
~
when i am executing ,it is giving error like :
socket(clients[0]).emit("message_to_client" , { message: data["message"] });
^
TypeError: object is not a function at Socket.
Share Improve this question asked Oct 6, 2014 at 12:49 user4113195user4113195 131 gold badge1 silver badge3 bronze badges1 Answer
Reset to default 3I think I understand what you are trying to do. You want to address a message to a particular socket by using its ID.
According to the documentation socket
is not a function (http://socket.io/docs/server-api/#socket), so invoking it like socket()
is causing your code to fail.
Instead of storing the sockets in an array, let's try storing them in a hash instead:
var clients = {};
io.sockets.on('connection', function (socket) {
// store a reference to this socket in the hash, using
// its id as the hash key
clients[socket.id] = socket;
socket.on('message_to_server', function (data) {
// look up a client socket by id (this would e from
// the client, and would need to be municated to the
// client in some way, perhaps with a broadcast message
// sent to every client whenever another client "logged in"
var destination = clients[data.destinationId];
// if destination is undefined (falsy) it does not
// exist in the hash
if (!destination) {
return;
}
// send a message to the destination
destination.emit("message_to_client" , { message: data["message"] });
});
});
If you only want to send a message to the same socket created during connection, you don't have to store a reference to it in a hash or array, because you have access to it within the closure:
io.sockets.on('connection', function (socket) {
socket.on('message_to_server', function (data) {
// we have a reference to socket from the closure above ^
socket.emit("message_to_client" , { message: data["message"] });
});
});