socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Socket.io not calling connection

Open iamdriz opened this issue 11 years ago • 3 comments

I'm trying the following in a NodeJS application:

var io = require('socket.io')();

io.on('connection', function (socket) {
    console.log('connected');
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
        console.log(data);
    });
});

io.listen(8004);

var socket = require('socket.io/node_modules/socket.io-client')('http://localhost');

socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
});

But I don't see any of the console logs.

Am I doing something wrong? I don't even see the connected log and don't get any errors.

iamdriz avatar Jul 21 '15 16:07 iamdriz

If I console out io after the require, I get:

{ nsps:
   { '/':
      { name: '/',
        server: [Circular],
        sockets: [],
        connected: {},
        fns: [],
        ids: 0,
        acks: {},
        adapter: [Object] } },
  _path: '/socket.io',
  _serveClient: true,
  _adapter: [Function: Adapter],
  _origins: '*:*',
  sockets:
   { name: '/',
     server: [Circular],
     sockets: [],
     connected: {},
     fns: [],
     ids: 0,
     acks: {},
     adapter: { nsp: [Circular], rooms: {}, sids: {}, encoder: {} } } }

So the module is being loaded.

And if I console log out io after I do the listen on port 8004 I get:

{ nsps:
   { '/':
      { name: '/',
        server: [Circular],
        sockets: [],
        connected: {},
        fns: [],
        ids: 0,
        acks: {},
        adapter: [Object],
        _events: [Object] } },
  _path: '/socket.io',
  _serveClient: true,
  _adapter: [Function: Adapter],
  _origins: '*:*',
  sockets:
   { name: '/',
     server: [Circular],
     sockets: [],
     connected: {},
     fns: [],
     ids: 0,
     acks: {},
     adapter: { nsp: [Circular], rooms: {}, sids: {}, e
     _events: { connection: [Function] } },
  eio:
   { clients: {},
     clientsCount: 0,
     pingTimeout: 60000,
     pingInterval: 25000,
     upgradeTimeout: 10000,
     maxHttpBufferSize: 100000000,
     transports: [ 'polling', 'websocket' ],
     allowUpgrades: true,
     allowRequest: [Function],
     cookie: 'io',
     ws:
      { domain: null,
        _events: {},
        _maxListeners: undefined,
        options: [Object],
        path: null,
        clients: [] },
     _events: { connection: [Function] } },
  httpServer:
   { domain: null,
     _events:
      { connection: [Function: connectionListener],
        clientError: [Function],
        close: [Function],
        upgrade: [Function],
        request: [Function] },
     _maxListeners: undefined,
     _connections: 0,
     _handle:
      { fd: undefined,
        reading: false,
        owner: [Circular],
        onread: null,
        onconnection: [Function: onconnection],
        writeQueueSize: 0 },
     _usingSlaves: false,
     _slaves: [],
     allowHalfOpen: true,
     pauseOnConnect: false,
     httpAllowHalfOpen: false,
     timeout: 120000,
     _connectionKey: '4:null:8004' },
  engine:
   { clients: {},
     clientsCount: 0,
     pingTimeout: 60000,
     pingInterval: 25000,
     upgradeTimeout: 10000,
     maxHttpBufferSize: 100000000,
     transports: [ 'polling', 'websocket' ],
     allowUpgrades: true,
     allowRequest: [Function],
     cookie: 'io',
     ws:
      { domain: null,
        _events: {},
        _maxListeners: undefined,
        options: [Object],
        path: null,
        clients: [] },
     _events: { connection: [Function] } } }

iamdriz avatar Jul 21 '15 16:07 iamdriz

The url in var socket = .... should be http://localhost:8004

maxzeroedge avatar Jul 21 '15 16:07 maxzeroedge

Ah right, and the connection event only fires when both client and server have connected rather than just the server end. Thanks.

iamdriz avatar Jul 21 '15 17:07 iamdriz