0

I've seen many similar questions here, like this one about connecting servers to sockets, however, that doesn't seem to be working.

My goal is to have one of my server pages occasionally emit an "advance" event to all of the connected clients. My sockets server listener (sockets.js) looks like this

io.on('connection', function (socket) {
    socket.on('advance', function () {
        console.log('advancing clients');
        io.emit('advance');
    });
});

and my other server page (queue.js) has this code

var socket = require('socket.io-client');
socket.connect('localhost:5000');
socket.on('connect', function () {
    socket.emit('advance');
});

I have also tried

var socket = require('socket.io-client');
socket.connect('localhost:5000');
socket.emit('advance');

Both of those were in the other question I linked, however both of those failed. In the case of the first example, I received the error

socket.on is not a function

and on the second

socket.emit is not a function

it would seem that socket.connect worked, however, socket.on and socket.emit both failed. The idea is that queue.js will emit an event to all the clients, so they know to request the next song from the server.

As bonus points, I happen to be using passport.socketio to authenticate my users (though I don't seem to be getting the "ioAuthFail" message that would normally occur when a user attempts to connect and is not authenticated).

for what it's worth, my socket authentication code looks like

io.use(passportSocketIo.authorize({
    //cookieParser:
    key:'user',
    secret:'secret',
    store:new MongoStore({mongooseConnection:mongoose.connection}),
    success:onAuthorizeSuccess,
    fail:function(){console.log('ioAuthFail')}
}));

If there's a better way to advance the clients than using socket.io, i would also accept that, but for now it seems like this is the best way for me, assuming I can get this working.

EDIT as mentioned through comments, changing my code to

var io = require('socket.io-client');
socket = io.connect('localhost:5000');
socket.on('connect', function () {
    socket.emit('advance');
});
socket.emit('advance');

doesn't break it, but it doesn't work, either.

Community
  • 1
  • 1
nickrenfo2
  • 85
  • 1
  • 10
  • Try `var io = require('socket.io-client'); var socket = io.connect(); socket.on(…` – laggingreflex Oct 24 '15 at 17:16
  • i change my code to `socket = io.connect('localhost:5000');` `socket.on('connect', function () {` `socket.emit('advance');` `});` `socket.emit('advance');` That doesn't seem to break it, but i'm not recieving any console.log from my socket server – nickrenfo2 Oct 24 '15 at 17:45

0 Answers0