Mario
Mario

Reputation: 2739

Socket.io Error (socket.send is not a function)

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));

Upvotes: 0

Views: 12220

Answers (3)

Atinux
Atinux

Reputation: 1703

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>
    

Upvotes: 2

RobertPitt
RobertPitt

Reputation: 57268

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

Upvotes: 1

fent
fent

Reputation: 18215

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);
});

Upvotes: 0

Related Questions