title: How to listen events with Node.JS & Socket.io
body: <p>I am making online game server with Node.js, but I am stuck at event listening.</p>
  
  <p>In table.js I wanna listen to "test" emit from client. But it looks nothing.</p>
  
  <pre><code>var socket = io.sockets.connected[socket_id]
  socket.on("test", function (d) {
     console.log(d)
  })
  </code></pre>
  
  <p>I am emit from table.js like this (it is working without problem)</p>
  
  <pre><code>   io.sockets.connected[socket_id].emit(event_name, data)
  </code></pre>
  
  <p>This is also result of <em>console.log( io.sockets.connected[socket_id] )</em></p>
  
  <pre><code>Socket {
    nsp: 
     Namespace {
       name: '/',
       server: 
        Server {
          nsps: [Object],
          _path: '/socket.io',
          _serveClient: true,
          _adapter: [Function: Adapter],
          _origins: '*:*',
          sockets: [Circular],
          eio: [Object],
          httpServer: [Object],
          engine: [Object] },
       sockets: 
        { '/#8CUB-zmn20g-hxxjAAAA': [Object],
          '/#FevFALjTPcIkP9EvAAAB': [Object],
          '/#z5-YdxwrsIayv7yuAAAD': [Circular] },
       connected: 
        { '/#8CUB-zmn20g-hxxjAAAA': [Object],
          '/#FevFALjTPcIkP9EvAAAB': [Object],
          '/#z5-YdxwrsIayv7yuAAAD': [Circular] },
       fns: [ [Function] ],
       ids: 0,
       adapter: 
        Adapter {
          nsp: [Circular],
          rooms: [Object],
          sids: [Object],
          encoder: Encoder {} },
       _events: { connection: [Function] },
       _eventsCount: 1 },
    server: 
     Server {
       nsps: { '/': [Object] },
       _path: '/socket.io',
       _serveClient: true,
       _adapter: [Function: Adapter],
       _origins: '*:*',
       sockets: 
        Namespace {
          name: '/',
          server: [Circular],
          sockets: [Object],
          connected: [Object],
          fns: [Object],
          ids: 0,
          adapter: [Object],
          _events: [Object],
          _eventsCount: 1 },
       eio: 
        Server {
          clients: [Object],
          clientsCount: 3,
          pingTimeout: 60000,
          pingInterval: 25000,
          upgradeTimeout: 10000,
          maxHttpBufferSize: 100000000,
          transports: [Object],
          allowUpgrades: true,
          allowRequest: [Function: bound ],
          cookie: 'io',
          cookiePath: false,
          perMessageDeflate: [Object],
          httpCompression: [Object],
          ws: [Object],
          _events: [Object],
          _eventsCount: 1 },
       httpServer: 
        Server {
          domain: null,
          _events: [Object],
          _eventsCount: 5,
          _maxListeners: undefined,
          _connections: 9,
          _handle: [Object],
          _usingSlaves: false,
          _slaves: [],
          _unref: false,
          allowHalfOpen: true,
          pauseOnConnect: false,
          httpAllowHalfOpen: false,
          timeout: 120000,
          _pendingResponseData: 0,
          _connectionKey: '6::::5555' },
       engine: 
        Server {
          clients: [Object],
          clientsCount: 3,
          pingTimeout: 60000,
          pingInterval: 25000,
          upgradeTimeout: 10000,
          maxHttpBufferSize: 100000000,
          transports: [Object],
          allowUpgrades: true,
          allowRequest: [Function: bound ],
          cookie: 'io',
          cookiePath: false,
          perMessageDeflate: [Object],
          httpCompression: [Object],
          ws: [Object],
          _events: [Object],
          _eventsCount: 1 } },
    adapter: 
     Adapter {
       nsp: 
        Namespace {
          name: '/',
          server: [Object],
          sockets: [Object],
          connected: [Object],
          fns: [Object],
          ids: 0,
          adapter: [Circular],
          _events: [Object],
          _eventsCount: 1 },
       rooms: 
        { '/#8CUB-zmn20g-hxxjAAAA': [Object],
          '/#FevFALjTPcIkP9EvAAAB': [Object],
          '/#z5-YdxwrsIayv7yuAAAD': [Object] },
       sids: 
        { '/#8CUB-zmn20g-hxxjAAAA': [Object],
          '/#FevFALjTPcIkP9EvAAAB': [Object],
          '/#-VB9E9ehK70lcFOIAAAC': {},
          '/#z5-YdxwrsIayv7yuAAAD': [Object] },
       encoder: Encoder {} },
    id: '/#z5-YdxwrsIayv7yuAAAD',
    client: 
     Client {
       server: 
        Server {
          nsps: [Object],
          _path: '/socket.io',
          _serveClient: true,
          _adapter: [Function: Adapter],
          _origins: '*:*',
          sockets: [Object],
          eio: [Object],
          httpServer: [Object],
          engine: [Object] },
       conn: 
        Socket {
          id: 'z5-YdxwrsIayv7yuAAAD',
          server: [Object],
          upgrading: false,
          upgraded: true,
          readyState: 'open',
          writeBuffer: [],
          packetsFn: [],
          sentCallbackFn: [Object],
          cleanupFn: [Object],
          request: [Object],
          remoteAddress: '::ffff:127.0.0.1',
          checkIntervalTimer: null,
          upgradeTimeoutTimer: null,
          pingTimeoutTimer: [Object],
          transport: [Object],
          _events: [Object],
          _eventsCount: 3 },
       encoder: Encoder {},
       decoder: Decoder { reconstructor: null, _callbacks: [Object] },
       id: 'z5-YdxwrsIayv7yuAAAD',
       request: 
        IncomingMessage {
          _readableState: [Object],
          readable: false,
          domain: null,
          _events: {},
          _eventsCount: 0,
          _maxListeners: undefined,
          socket: [Object],
          connection: [Object],
          httpVersionMajor: 1,
          httpVersionMinor: 1,
          httpVersion: '1.1',
          complete: true,
          headers: [Object],
          rawHeaders: [Object],
          trailers: {},
          rawTrailers: [],
          upgrade: false,
          url: '/socket.io/?EIO=3&amp;transport=polling&amp;t=LCK8z9f',
          method: 'GET',
          statusCode: null,
          statusMessage: null,
          client: [Object],
          _consuming: true,
          _dumped: true,
          _query: [Object],
          res: [Object],
          cleanup: [Function: cleanup],
          session_id: undefined,
          read: [Function] },
       onclose: [Function: bound ],
       ondata: [Function: bound ],
       onerror: [Function: bound ],
       ondecoded: [Function: bound ],
       sockets: { '/#z5-YdxwrsIayv7yuAAAD': [Circular] },
       nsps: { '/': [Circular] },
       connectBuffer: [] },
    conn: 
     Socket {
       id: 'z5-YdxwrsIayv7yuAAAD',
       server: 
        Server {
          clients: [Object],
          clientsCount: 3,
          pingTimeout: 60000,
          pingInterval: 25000,
          upgradeTimeout: 10000,
          maxHttpBufferSize: 100000000,
          transports: [Object],
          allowUpgrades: true,
          allowRequest: [Function: bound ],
          cookie: 'io',
          cookiePath: false,
          perMessageDeflate: [Object],
          httpCompression: [Object],
          ws: [Object],
          _events: [Object],
          _eventsCount: 1 },
       upgrading: false,
       upgraded: true,
       readyState: 'open',
       writeBuffer: [],
       packetsFn: [],
       sentCallbackFn: [ undefined ],
       cleanupFn: [ [Function], [Function] ],
       request: 
        IncomingMessage {
          _readableState: [Object],
          readable: false,
          domain: null,
          _events: {},
          _eventsCount: 0,
          _maxListeners: undefined,
          socket: [Object],
          connection: [Object],
          httpVersionMajor: 1,
          httpVersionMinor: 1,
          httpVersion: '1.1',
          complete: true,
          headers: [Object],
          rawHeaders: [Object],
          trailers: {},
          rawTrailers: [],
          upgrade: false,
          url: '/socket.io/?EIO=3&amp;transport=polling&amp;t=LCK8z9f',
          method: 'GET',
          statusCode: null,
          statusMessage: null,
          client: [Object],
          _consuming: true,
          _dumped: true,
          _query: [Object],
          res: [Object],
          cleanup: [Function: cleanup],
          session_id: undefined,
          read: [Function] },
       remoteAddress: '::ffff:127.0.0.1',
       checkIntervalTimer: null,
       upgradeTimeoutTimer: null,
       pingTimeoutTimer: 
        { _called: false,
          _idleTimeout: 85000,
          _idlePrev: [Object],
          _idleNext: [Object],
          _idleStart: 9247,
          _onTimeout: [Function],
          _repeat: null },
       transport: 
        WebSocket {
          readyState: 'open',
          discarded: false,
          socket: [Object],
          writable: false,
          perMessageDeflate: [Object],
          supportsBinary: true,
          _events: [Object],
          _eventsCount: 4 },
       _events: 
        { close: [Object],
          data: [Function: bound ],
          error: [Function: bound ] },
       _eventsCount: 3 },
    rooms: { '/#z5-YdxwrsIayv7yuAAAD': '/#z5-YdxwrsIayv7yuAAAD' },
    acks: {},
    connected: true,
    disconnected: false,
    handshake: 
     { headers: 
        { host: '127.0.0.1:5555',
          connection: 'keep-alive',
          'cache-control': 'max-age=0',
          'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
          accept: '*/*',
          dnt: '1',
          referer: 'http://127.0.0.1:5555/',
          'accept-encoding': 'gzip, deflate, sdch',
          'accept-language': 'tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4,bg;q=0.2,it;q=0.2,fr;q=0.2',
          cookie: 'io=-VB9E9ehK70lcFOIAAAC' },
       time: 'Wed Feb 24 2016 18:36:45 GMT+0200 (EET)',
       address: '::ffff:127.0.0.1',
       xdomain: false,
       secure: false,
       issued: 1456331805312,
       url: '/socket.io/?EIO=3&amp;transport=polling&amp;t=LCK8z9f',
       query: { EIO: '3', transport: 'polling', t: 'LCK8z9f' } },
    _events: { giris: [Function], oyna: [Function], disconnect: [Function] },
    _eventsCount: 3 }
  </code></pre>
  
  <p>Please help me, thanks.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>First I am making table(4 people) like this.</p>
  
  <pre><code>new Game(io, id)
  </code></pre>
  
  <p>And my Game model.</p>
  
  <pre><code>function Game(io, id) {
    this.id = id
    this.io = io
    this.players = []
    this.status = 0
    // ...
  }
  </code></pre>