Writing to sockets


I have trouble understanding the reason for the error I get when the user tries to write anything to the server:

TypeError: Object #<identifyClient> has no method 'write'
at writeToAll (/root/node/mud/server.js:13:15)

Why does identifyClient() complaints about the write(), while it happens in the writeToAll() (line 15 has comment next to it)? identifyClient() really only sets the name for the client, and should not be concerned what happens in the writeToAll().

var net = require("net");

var clients = [];

function identifyClient(client) {
    this.name = null;
    this.client = client;

function writeToAll(data, client) {
    for (var i = 0; i < clients.length; i++) {
        if (clients[i] != client) {
            clients[i].write(data); // This is line 15

var server = net.createServer(function(client) {
    var clientID = new identifyClient(client);

    client.on("data", function(data) {
        writeToAll(data, client);

Problem courtesy of: Eleeist


Replace clients[i] with clients[i].client

also you have to remove the client from the clients array once it disconnects.

Solution courtesy of: stewe


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.