0

I'm trying out Websockets/Node.js/Socket.io/Express for the first time and I'm trying to create a simple chat program. Everything runs fine and I see both clients in my node termial.

But when I try to execute my socket.send(), I get an error in Firefox (socket.send is not a function). It doesn't complain about socket.connect() so I know the socket.io.js is loaded.

Here is my server code:

var sys = require('util');
var express = require('express');
var io = require('socket.io');

var app = express.createServer();
app.listen(8080);
app.use(express.static(__dirname));

app.get('/', function (req, res) {
    res.render('index.html', {
        title: 'Chat'
    });
});

var socket = io.listen(app);

socket.on('connection', function (client) {
    client.on('message', function (message) {
        console.log("Message: " + JSON.stringify(data));
        socket.broadcast(message);
    });
    client.on('disconnect', function () {});
});

My client code:

<script src="http://localhost:8080/socket.io/socket.io.js"></script>

var socket = new io.Socket("http://localhost:8080");
socket.connect();

Then I do some code to get the chat message and send it.

socket.send(JSON.stringify(values));
Bardi Harborow
  • 1,803
  • 1
  • 28
  • 41
Mario
  • 2,721
  • 1
  • 22
  • 23
  • What version of Socket.IO do you have? – alessioalex Dec 22 '11 at 21:40
  • @alessioalex io.version = '0.8.7'; I tried emit() which doesn't give an error but it doesn't send anything :S – Mario Dec 22 '11 at 22:07
  • This has been covered in other stackoverflow q/a cf => http://stackoverflow.com/questions/7386984/socket-io-client-not-receiving/8603282#8603282, http://stackoverflow.com/questions/7627626/socket-io-not-sending-a-message-to-all-connected-sockets/8593305#8593305, http://stackoverflow.com/questions/8510260/node-js-server-is-not-working-properly/8593810#8593810, http://stackoverflow.com/questions/7069202/info-unhandled-socket-io-url/8593409#8593409, example using send - https://github.com/parj/node-websocket-demo, example using emit - https://github.com/parj/node-websocket-demo/tree/socket_emit – First Zero Dec 23 '11 at 14:02

3 Answers3

2

Explanations

You haven't initialized Socket.io correctly on the server-side and client-side.

  • Client Side

    1. new io.Socket("http://localhost:8080"); doesn't give you the object that you want, you need new io.connect("http://localhost:8080");.

    2. You need to wait until the client is connected to the server before sending a message.

  • Server side

    1. socket is the object send back by Socket.IO, you need to use socket.sockets to have access to on.

    2. To broadcast a message, you need to use the client object like this: client.broadcast.send()

    3. The variable data doesn't exist on your broadcast. You probably mean message.

Solution

  • Server

    var sys = require('util'),
        express = require('express'),
        io = require('socket.io'),
        app = express.createServer();
    
    app.listen(8080);
    
    app.use(express.static(__dirname));
    
    app.get('/', function (req, res) {
        res.render('index.html', {
            title: 'Chat'
        });
    });
    
    var io = io.listen(app);
    
    io.sockets.on('connection', function (client) {
        client.on('message', function (message) {
            console.log("Message: " + JSON.stringify(message));
            client.broadcast.send(message);
        });
    
        client.on('disconnect', function () {});
    });
    
  • Client

    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
    <script>
        var socket = new io.connect("http://localhost:8080"),
            connected = false;
    
        socket.on('connect', function () {
            connected = true;
        });
    
        // Use this in your chat function.
        if (connected) {
            socket.send(JSON.stringify(values));
        }
    </script>
    
Bardi Harborow
  • 1,803
  • 1
  • 28
  • 41
Atinux
  • 1,703
  • 15
  • 18
1

socket.broadcast(message); should be io.sockets.emit('key', message);

when you use the socket object passed in threw the connect event your only emitting information to that client, to emit to all clients you have to use io.sockets.emit().

also with socket.send(JSON.stringify(values)); I think you want to do socket.emit(namespace, data);

see my connection file from one of my projects here: https://github.com/AdminSpot/HangoutCanopy/blob/master/javascripts/connection.js

RobertPitt
  • 56,863
  • 21
  • 114
  • 161
0

You have to wait for socket.io to connect on the client side

var socket = new io.Socket("http://localhost:8080");
socket.connect();
socket.on('connect', function() {
  socket.emit('event', data);
});
fent
  • 17,861
  • 15
  • 87
  • 91