1

There is a code line:

io.sockets.socket(socketid).emit('counter', { all : data.all, dialog : data.dialog });

It gives me exception:

TypeError: Object #<Namespace> has no method 'socket'

My connection event:

io.sockets.on('connection', function(socket) {});

Why object socket is no?

Full code:

var socketio = require('socket.io');
var server = http.createServer(app).listen(8181, function(){
    console.log("Express server listening on port "+ app.get('port') +" in "+ app.get('env') +" mode.");
});

var io = socketio.listen(server);
io.on('connection', function(socket) {});
AllenDegrud
  • 189
  • 2
  • 13

1 Answers1

3

Socket server creation for use with express. Look at http://socket.io/docs/ for more information and help.

var app = require('express').createServer();
var io = require('socket.io')(app);

app.listen(8181);

io.on('connect', function(socket) {
    socket.on('example', function(data) {
        //parse the data, do something, create response data object, etc.

    });
});

And with HTTP + Express

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(8181);

io.on('connect', function(socket) {
        socket.on('example', function(data) {
            //parse the data, do something, create response data object, etc.

        });
    });

For the emit you are trying to create (my below example will broadcast only to the connected socket, by providing the .to(socket.id) )

io.to(socket.id).emit('counter', {all : data.all, dialog : data.dialog});
Olivercodes
  • 1,048
  • 5
  • 17
  • i tried your example witout `io.sockets` - the error is same – AllenDegrud Oct 28 '14 at 20:45
  • Do you have io pulled into your server? `var io = require('socket.io');` This would be within your server.js file. – Olivercodes Oct 28 '14 at 20:52
  • Yes, in a top script there is: `socketio = require('socket.io');` `var io = socketio.listen(server);` – AllenDegrud Oct 28 '14 at 20:54
  • Ok, this may seem like a trivial fix, but humor me. Looking at the latest socket server we wrote, I have the following setup code: `var io = require('socket.io');` and `var server = require('https').createServer(options, app).listen(port); // remember thy port require('./config/sockets').start(io.listen(server));` You can use just io.listen(server) with this code (we have our socket code abstracted into config/sockets). – Olivercodes Oct 28 '14 at 21:02
  • I think you were missing a couple of things in your full code there, such as the express require. Look at my full code example above, and then I strongly suggest the socket.io docs link I have provided. – Olivercodes Oct 28 '14 at 21:32
  • So, your code is running, but error is too at: `io.sockets.socket(socketid).emit` (: – AllenDegrud Oct 28 '14 at 21:50
  • Coool...problem is resolved. This answers helped me. I use socket.io version 1.0. http://stackoverflow.com/questions/24221820/io-sockets-socketsocket-id-emit-has-no-method-socket – AllenDegrud Oct 28 '14 at 21:53
  • I could make an argument for why my solution is more elegant :). "Each Socket in Socket.IO is identified by a random, unguessable, unique identifier Socket#id. For your convenience, each socket automatically joins a room identified by this id." So you can just emit to the socket.id (as I provided in my answer above!). Cheers for working! – Olivercodes Oct 28 '14 at 22:08