Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to get socket information from connection event in Node.js

1 Answer

0 votes
const http = require('http');
const server = http.createServer();

server.on('connection', (socket) => {
    console.log("connection:", socket);
});

server.listen(8080);

console.log('Listening on port 8080');

// To run: Open your web browser with the URL: http://localhost:8080/



/*
run:
   
Listening on port 8080
connection: Socket {
  connecting: false,
  _hadError: false,
  _parent: null,
  _host: null,
  _readableState: ReadableState {
    objectMode: false,
    highWaterMark: 16384,
    buffer: BufferList { head: null, tail: null, length: 0 },
    length: 0,
    pipes: null,
    pipesCount: 0,
    flowing: true,
    ended: false,
    endEmitted: false,
    reading: true,
    sync: false,
    needReadable: true,
    emittedReadable: false,
    readableListening: false,
    resumeScheduled: true,
    emitClose: false,
    autoDestroy: false,
    destroyed: false,
    defaultEncoding: 'utf8',
    awaitDrain: 0,
    readingMore: false,
    decoder: null,
    encoding: null,
    [Symbol(kPaused)]: false
  },
  readable: true,
  _events: [Object: null prototype] {
    end: [ [Function: onReadableStreamEnd], [Function: bound socketOnEnd] ],
    timeout: [Function: socketOnTimeout],
    data: [Function: bound socketOnData],
    error: [Function: socketOnError],
    close: [Function: bound socketOnClose],
    drain: [Function: bound socketOnDrain],
    resume: [Function: onSocketResume],
    pause: [Function: onSocketPause]
  },
  _eventsCount: 8,
  _maxListeners: undefined,
  _writableState: WritableState {
    objectMode: false,
    highWaterMark: 16384,
    finalCalled: false,
    needDrain: false,
    ending: false,
    ended: false,
    finished: false,
    destroyed: false,
    decodeStrings: false,
    defaultEncoding: 'utf8',
    length: 0,
    writing: false,
    corked: 0,
    sync: true,
    bufferProcessing: false,
    onwrite: [Function: bound onwrite],
    writecb: null,
    writelen: 0,
    afterWriteTickInfo: null,
    bufferedRequest: null,
    lastBufferedRequest: null,
    pendingcb: 0,
    prefinished: false,
    errorEmitted: false,
    emitClose: false,
    autoDestroy: false,
    bufferedRequestCount: 0,
    corkedRequestsFree: {
      next: null,
      entry: null,
      finish: [Function: bound onCorkedFinish]
    }
  },
  writable: true,
  allowHalfOpen: true,
  _sockname: null,
  _pendingData: null,
  _pendingEncoding: '',
  server: Server {
    insecureHTTPParser: undefined,
    _events: [Object: null prototype] { connection: [Array] },
    _eventsCount: 1,
    _maxListeners: undefined,
    _connections: 1,
    _handle: TCP {
      reading: false,
      onconnection: [Function: onconnection],
      [Symbol(owner)]: [Circular]
    },
    _usingWorkers: false,
    _workers: [],
    _unref: false,
    allowHalfOpen: true,
    pauseOnConnect: false,
    httpAllowHalfOpen: false,
    timeout: 120000,
    keepAliveTimeout: 5000,
    maxHeadersCount: null,
    headersTimeout: 40000,
    _connectionKey: '6::::8080',
    [Symbol(IncomingMessage)]: [Function: IncomingMessage],
    [Symbol(ServerResponse)]: [Function: ServerResponse],
    [Symbol(kCapture)]: false,
    [Symbol(asyncId)]: 2
  },
  _server: Server {
    insecureHTTPParser: undefined,
    _events: [Object: null prototype] { connection: [Array] },
    _eventsCount: 1,
    _maxListeners: undefined,
    _connections: 1,
    _handle: TCP {
      reading: false,
      onconnection: [Function: onconnection],
      [Symbol(owner)]: [Circular]
    },
    _usingWorkers: false,
    _workers: [],
    _unref: false,
    allowHalfOpen: true,
    pauseOnConnect: false,
    httpAllowHalfOpen: false,
    timeout: 120000,
    keepAliveTimeout: 5000,
    maxHeadersCount: null,
    headersTimeout: 40000,
    _connectionKey: '6::::8080',
    [Symbol(IncomingMessage)]: [Function: IncomingMessage],
    [Symbol(ServerResponse)]: [Function: ServerResponse],
    [Symbol(kCapture)]: false,
    [Symbol(asyncId)]: 2
  },
  timeout: 120000,
  parser: HTTPParser {
    '0': [Function: parserOnHeaders],
    '1': [Function: parserOnHeadersComplete],
    '2': [Function: parserOnBody],
    '3': [Function: parserOnMessageComplete],
    '4': [Function: bound onParserExecute],
    _headers: [],
    _url: '',
    socket: [Circular],
    incoming: null,
    outgoing: null,
    maxHeaderPairs: 2000,
    _consumed: true,
    onIncoming: [Function: bound parserOnIncoming],
    parsingHeadersStart: 1583167550713
  },
  on: [Function: socketListenerWrap],
  addListener: [Function: socketListenerWrap],
  prependListener: [Function: socketListenerWrap],
  _paused: false,
  [Symbol(asyncId)]: 6,
  [Symbol(kHandle)]: TCP {
    reading: true,
    onconnection: null,
    _consumed: true,
    [Symbol(owner)]: [Circular]
  },
  [Symbol(lastWriteQueueSize)]: 0,
  [Symbol(timeout)]: Timeout {
    _idleTimeout: 120000,
    _idlePrev: [TimersList],
    _idleNext: [TimersList],
    _idleStart: 18719,
    _onTimeout: [Function: bound ],
    _timerArgs: undefined,
    _repeat: null,
    _destroyed: false,
    [Symbol(refed)]: false,
    [Symbol(asyncId)]: 7,
    [Symbol(triggerId)]: 6
  },
  [Symbol(kBuffer)]: null,
  [Symbol(kBufferCb)]: null,
  [Symbol(kBufferGen)]: null,
  [Symbol(kCapture)]: false,
  [Symbol(kBytesRead)]: 0,
  [Symbol(kBytesWritten)]: 0
}
   
*/

 



answered Mar 2, 2020 by avibootz
edited Mar 2, 2020 by avibootz

Related questions

1 answer 229 views
1 answer 139 views
1 answer 190 views
1 answer 164 views
164 views asked Mar 10, 2020 by avibootz
1 answer 208 views
3 answers 219 views
...