Packages

class Socket extends Object with StObject

This class is an abstraction of a TCP socket or a streaming IPC endpoint (uses named pipes on Windows, and Unix domain sockets otherwise). It is also an EventEmitter.

A net.Socket can be created by the user and used directly to interact with a server. For example, it is returned by createConnection, so the user can use it to talk to the server.

It can also be created by Node.js and passed to the user when a connection is received. For example, it is passed to the listeners of a 'connection' event emitted on a Server, so the user can use it to interact with the client.

Annotations
@JSType() @JSImport("net", "Socket") @native()
Since

v0.3.4

Linear Supertypes
StObject, Object, Any, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Socket
  2. StObject
  3. Object
  4. Any
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Instance Constructors

  1. new Socket(options: SocketConstructorOpts)
  2. new Socket()

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. def addListener(event: String, listener: Function1[Any, Unit]): Socket.this.type

    events.EventEmitter

    events.EventEmitter

    1. close 2. connect 3. data 4. drain 5. end 6. error 7. lookup 8. timeout
  5. def addListener_close(event: close, listener: Function1[Boolean, Unit]): Socket.this.type
    Annotations
    @JSName("addListener")
  6. def addListener_connect(event: connect, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("addListener")
  7. def addListener_data(event: data, listener: Function1[Buffer, Unit]): Socket.this.type
    Annotations
    @JSName("addListener")
  8. def addListener_drain(event: drain, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("addListener")
  9. def addListener_end(event: end, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("addListener")
  10. def addListener_error(event: error, listener: Function1[Error, Unit]): Socket.this.type
    Annotations
    @JSName("addListener")
  11. def addListener_lookup(event: lookup, listener: Function4[Error, String, |[String, Double], String, Unit]): Socket.this.type
    Annotations
    @JSName("addListener")
  12. def addListener_ready(event: ready, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("addListener")
  13. def addListener_timeout(event: timeout, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("addListener")
  14. def address(): |[AddressInfo, Object]

    Returns the bound address, the address family name and port of the socket as reported by the operating system:{ port: 12346, family: 'IPv4', address: '127.0.0.1' }

    Returns the bound address, the address family name and port of the socket as reported by the operating system:{ port: 12346, family: 'IPv4', address: '127.0.0.1' }

    Since

    v0.1.90

  15. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  16. val bytesRead: Double

    The amount of received bytes.

    The amount of received bytes.

    Since

    v0.5.3

  17. val bytesWritten: Double

    The amount of bytes sent.

    The amount of bytes sent.

    Since

    v0.5.3

  18. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
  19. def connect(port: Double, host: String, connectionListener: Function0[Unit]): Socket.this.type
  20. def connect(port: Double, host: String): Socket.this.type
  21. def connect(port: Double, connectionListener: Function0[Unit]): Socket.this.type
  22. def connect(port: Double): Socket.this.type
  23. def connect(path: String, connectionListener: Function0[Unit]): Socket.this.type
  24. def connect(path: String): Socket.this.type
  25. def connect(options: SocketConnectOpts, connectionListener: Function0[Unit]): Socket.this.type
  26. def connect(options: SocketConnectOpts): Socket.this.type

    Initiate a connection on a given socket.

    Initiate a connection on a given socket.

    Possible signatures:

    * socket.connect(options[, connectListener]) * socket.connect(path[, connectListener]) for IPC connections. * socket.connect(port[, host][, connectListener]) for TCP connections. * Returns: net.Socket The socket itself.

    This function is asynchronous. When the connection is established, the 'connect' event will be emitted. If there is a problem connecting, instead of a 'connect' event, an 'error' event will be emitted with the error passed to the 'error' listener. The last parameter connectListener, if supplied, will be added as a listener for the 'connect' event **once**.

    This function should only be used for reconnecting a socket after'close' has been emitted or otherwise it may lead to undefined behavior.

  27. val connecting: Boolean

    If true,socket.connect(options[, connectListener]) was called and has not yet finished.

    If true,socket.connect(options[, connectListener]) was called and has not yet finished. It will stay true until the socket becomes connected, then it is set to false and the 'connect' event is emitted. Note that the socket.connect(options[, connectListener]) callback is a listener for the 'connect' event.

    Since

    v6.1.0

  28. val destroyed: Boolean

    See writable.destroyed for further details.

  29. def emit(event: Symbol, args: Any*): Boolean
  30. def emit(event: String, args: Any*): Boolean
  31. def emit_close(event: close, hadError: Boolean): Boolean
    Annotations
    @JSName("emit")
  32. def emit_connect(event: connect): Boolean
    Annotations
    @JSName("emit")
  33. def emit_data(event: data, data: Buffer): Boolean
    Annotations
    @JSName("emit")
  34. def emit_drain(event: drain): Boolean
    Annotations
    @JSName("emit")
  35. def emit_end(event: end): Boolean
    Annotations
    @JSName("emit")
  36. def emit_error(event: error, err: Error): Boolean
    Annotations
    @JSName("emit")
  37. def emit_lookup(event: lookup, err: Error, address: String, family: Double, host: String): Boolean
    Annotations
    @JSName("emit")
  38. def emit_lookup(event: lookup, err: Error, address: String, family: String, host: String): Boolean
    Annotations
    @JSName("emit")
  39. def emit_ready(event: ready): Boolean
    Annotations
    @JSName("emit")
  40. def emit_timeout(event: timeout): Boolean
    Annotations
    @JSName("emit")
  41. def end(str: Uint8Array, encoding: Unit, callback: Function0[Unit]): Unit
  42. def end(str: Uint8Array, encoding: BufferEncoding, callback: Function0[Unit]): Unit
  43. def end(str: Uint8Array, encoding: BufferEncoding): Unit
  44. def end(str: String, encoding: Unit, callback: Function0[Unit]): Unit
  45. def end(str: String, encoding: BufferEncoding, callback: Function0[Unit]): Unit
  46. def end(str: String, encoding: BufferEncoding): Unit
  47. def end(callback: Function0[Unit]): Unit
  48. def end(buffer: Uint8Array, callback: Function0[Unit]): Unit
  49. def end(buffer: Uint8Array): Unit
  50. def end(buffer: String, callback: Function0[Unit]): Unit
  51. def end(buffer: String): Unit
  52. def end(): Unit

    Half-closes the socket.

    Half-closes the socket. i.e., it sends a FIN packet. It is possible the server will still send some data.

    See writable.end() for further details.

    returns

    The socket itself.

    Since

    v0.1.90

  53. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  54. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  55. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  56. def hasOwnProperty(v: String): Boolean
    Definition Classes
    Object
  57. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  58. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  59. def isPrototypeOf(v: Object): Boolean
    Definition Classes
    Object
  60. val localAddress: UndefOr[String]

    The string representation of the local IP address the remote client is connecting on.

    The string representation of the local IP address the remote client is connecting on. For example, in a server listening on '0.0.0.0', if a client connects on '192.168.1.1', the value of socket.localAddress would be'192.168.1.1'.

    Since

    v0.9.6

  61. val localPort: UndefOr[Double]

    The numeric representation of the local port.

    The numeric representation of the local port. For example, 80 or 21.

    Since

    v0.9.6

  62. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  63. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  64. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  65. def on(event: String, listener: Function1[Any, Unit]): Socket.this.type
  66. def on_close(event: close, listener: Function1[Boolean, Unit]): Socket.this.type
    Annotations
    @JSName("on")
  67. def on_connect(event: connect, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("on")
  68. def on_data(event: data, listener: Function1[Buffer, Unit]): Socket.this.type
    Annotations
    @JSName("on")
  69. def on_drain(event: drain, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("on")
  70. def on_end(event: end, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("on")
  71. def on_error(event: error, listener: Function1[Error, Unit]): Socket.this.type
    Annotations
    @JSName("on")
  72. def on_lookup(event: lookup, listener: Function4[Error, String, |[String, Double], String, Unit]): Socket.this.type
    Annotations
    @JSName("on")
  73. def on_ready(event: ready, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("on")
  74. def on_timeout(event: timeout, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("on")
  75. def once(event: String, listener: Function1[Any, Unit]): Socket.this.type
  76. def once_close(event: close, listener: Function1[Boolean, Unit]): Socket.this.type
    Annotations
    @JSName("once")
  77. def once_connect(event: connect, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("once")
  78. def once_data(event: data, listener: Function1[Buffer, Unit]): Socket.this.type
    Annotations
    @JSName("once")
  79. def once_drain(event: drain, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("once")
  80. def once_end(event: end, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("once")
  81. def once_error(event: error, listener: Function1[Error, Unit]): Socket.this.type
    Annotations
    @JSName("once")
  82. def once_lookup(event: lookup, listener: Function4[Error, String, |[String, Double], String, Unit]): Socket.this.type
    Annotations
    @JSName("once")
  83. def once_ready(event: ready, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("once")
  84. def once_timeout(event: timeout, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("once")
  85. def pause(): Socket.this.type

    Pauses the reading of data.

    Pauses the reading of data. That is, 'data' events will not be emitted. Useful to throttle back an upload.

    returns

    The socket itself.

  86. def prependListener(event: String, listener: Function1[Any, Unit]): Socket.this.type
  87. def prependListener_close(event: close, listener: Function1[Boolean, Unit]): Socket.this.type
    Annotations
    @JSName("prependListener")
  88. def prependListener_connect(event: connect, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("prependListener")
  89. def prependListener_data(event: data, listener: Function1[Buffer, Unit]): Socket.this.type
    Annotations
    @JSName("prependListener")
  90. def prependListener_drain(event: drain, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("prependListener")
  91. def prependListener_end(event: end, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("prependListener")
  92. def prependListener_error(event: error, listener: Function1[Error, Unit]): Socket.this.type
    Annotations
    @JSName("prependListener")
  93. def prependListener_lookup(event: lookup, listener: Function4[Error, String, |[String, Double], String, Unit]): Socket.this.type
    Annotations
    @JSName("prependListener")
  94. def prependListener_ready(event: ready, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("prependListener")
  95. def prependListener_timeout(event: timeout, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("prependListener")
  96. def prependOnceListener(event: String, listener: Function1[Any, Unit]): Socket.this.type
  97. def prependOnceListener_close(event: close, listener: Function1[Boolean, Unit]): Socket.this.type
    Annotations
    @JSName("prependOnceListener")
  98. def prependOnceListener_connect(event: connect, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("prependOnceListener")
  99. def prependOnceListener_data(event: data, listener: Function1[Buffer, Unit]): Socket.this.type
    Annotations
    @JSName("prependOnceListener")
  100. def prependOnceListener_drain(event: drain, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("prependOnceListener")
  101. def prependOnceListener_end(event: end, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("prependOnceListener")
  102. def prependOnceListener_error(event: error, listener: Function1[Error, Unit]): Socket.this.type
    Annotations
    @JSName("prependOnceListener")
  103. def prependOnceListener_lookup(event: lookup, listener: Function4[Error, String, |[String, Double], String, Unit]): Socket.this.type
    Annotations
    @JSName("prependOnceListener")
  104. def prependOnceListener_ready(event: ready, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("prependOnceListener")
  105. def prependOnceListener_timeout(event: timeout, listener: Function0[Unit]): Socket.this.type
    Annotations
    @JSName("prependOnceListener")
  106. def propertyIsEnumerable(v: String): Boolean
    Definition Classes
    Object
  107. def ref(): Socket.this.type

    Opposite of unref(), calling ref() on a previously unrefed socket will_not_ let the program exit if it's the only socket left (the default behavior).

    Opposite of unref(), calling ref() on a previously unrefed socket will_not_ let the program exit if it's the only socket left (the default behavior). If the socket is refed calling ref again will have no effect.

    returns

    The socket itself.

    Since

    v0.9.1

  108. val remoteAddress: UndefOr[String]

    The string representation of the remote IP address.

    The string representation of the remote IP address. For example,'74.125.127.100' or '2001:4860:a005::68'. Value may be undefined if the socket is destroyed (for example, if the client disconnected).

    Since

    v0.5.10

  109. val remoteFamily: UndefOr[String]

    The string representation of the remote IP family.

    The string representation of the remote IP family. 'IPv4' or 'IPv6'.

    Since

    v0.11.14

  110. val remotePort: UndefOr[Double]

    The numeric representation of the remote port.

    The numeric representation of the remote port. For example, 80 or 21.

    Since

    v0.5.10

  111. def resume(): Socket.this.type

    Resumes reading after a call to socket.pause().

    Resumes reading after a call to socket.pause().

    returns

    The socket itself.

  112. def setEncoding(encoding: BufferEncoding): Socket.this.type
  113. def setEncoding(): Socket.this.type

    Set the encoding for the socket as a Readable Stream.

    Set the encoding for the socket as a Readable Stream. See readable.setEncoding() for more information.

    returns

    The socket itself.

    Since

    v0.1.90

  114. def setKeepAlive(enable: Unit, initialDelay: Double): Socket.this.type
  115. def setKeepAlive(enable: Boolean, initialDelay: Double): Socket.this.type
  116. def setKeepAlive(enable: Boolean): Socket.this.type
  117. def setKeepAlive(): Socket.this.type

    Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket.

    Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket.

    Set initialDelay (in milliseconds) to set the delay between the last data packet received and the first keepalive probe. Setting 0 forinitialDelay will leave the value unchanged from the default (or previous) setting.

    Enabling the keep-alive functionality will set the following socket options:

    * SO_KEEPALIVE=1 * TCP_KEEPIDLE=initialDelay * TCP_KEEPCNT=10 * TCP_KEEPINTVL=1

    returns

    The socket itself.

    Since

    v0.1.92

  118. def setNoDelay(noDelay: Boolean): Socket.this.type
  119. def setNoDelay(): Socket.this.type

    Enable/disable the use of Nagle's algorithm.

    Enable/disable the use of Nagle's algorithm.

    When a TCP connection is created, it will have Nagle's algorithm enabled.

    Nagle's algorithm delays data before it is sent via the network. It attempts to optimize throughput at the expense of latency.

    Passing true for noDelay or not passing an argument will disable Nagle's algorithm for the socket. Passing false for noDelay will enable Nagle's algorithm.

    returns

    The socket itself.

    Since

    v0.1.90

  120. def setTimeout(timeout: Double, callback: Function0[Unit]): Socket.this.type
  121. def setTimeout(timeout: Double): Socket.this.type

    Sets the socket to timeout after timeout milliseconds of inactivity on the socket.

    Sets the socket to timeout after timeout milliseconds of inactivity on the socket. By default net.Socket do not have a timeout.

    When an idle timeout is triggered the socket will receive a 'timeout' event but the connection will not be severed. The user must manually call socket.end() or socket.destroy() to end the connection.

    js socket.setTimeout(3000); socket.on('timeout', () => { console.log('socket timeout'); socket.end(); });

    If timeout is 0, then the existing idle timeout is disabled.

    The optional callback parameter will be added as a one-time listener for the 'timeout' event.

    returns

    The socket itself.

    Since

    v0.1.90

  122. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  123. def toLocaleString(): String
    Definition Classes
    Object
  124. def toString(): String
    Definition Classes
    AnyRef → Any
  125. def unref(): Socket.this.type

    Calling unref() on a socket will allow the program to exit if this is the only active socket in the event system.

    Calling unref() on a socket will allow the program to exit if this is the only active socket in the event system. If the socket is already unrefed callingunref() again will have no effect.

    returns

    The socket itself.

    Since

    v0.9.1

  126. def valueOf(): Any
    Definition Classes
    Object
  127. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  128. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  129. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  130. def write(str: Uint8Array, encoding: Unit, cb: Function1[UndefOr[Error], Unit]): Boolean
  131. def write(str: Uint8Array, encoding: BufferEncoding, cb: Function1[UndefOr[Error], Unit]): Boolean
  132. def write(str: Uint8Array, encoding: BufferEncoding): Boolean
  133. def write(str: String, encoding: Unit, cb: Function1[UndefOr[Error], Unit]): Boolean
  134. def write(str: String, encoding: BufferEncoding, cb: Function1[UndefOr[Error], Unit]): Boolean
  135. def write(str: String, encoding: BufferEncoding): Boolean
  136. def write(buffer: Uint8Array, cb: Function1[UndefOr[Error], Unit]): Boolean
  137. def write(buffer: Uint8Array): Boolean

    Sends data on the socket.

    Sends data on the socket. The second parameter specifies the encoding in the case of a string. It defaults to UTF8 encoding.

    Returns true if the entire data was flushed successfully to the kernel buffer. Returns false if all or part of the data was queued in user memory.'drain' will be emitted when the buffer is again free.

    The optional callback parameter will be executed when the data is finally written out, which may not be immediately.

    See Writable stream write() method for more information.

    Since

    v0.1.90

  138. def write(buffer: String, cb: Function1[UndefOr[Error], Unit]): Boolean
  139. def write(buffer: String): Boolean

Deprecated Value Members

  1. val bufferSize: Double

    This property shows the number of characters buffered for writing.

    This property shows the number of characters buffered for writing. The buffer may contain strings whose length after encoding is not yet known. So this number is only an approximation of the number of bytes in the buffer.

    net.Socket has the property that socket.write() always works. This is to help users get up and running quickly. The computer cannot always keep up with the amount of data that is written to a socket. The network connection simply might be too slow. Node.js will internally queue up the data written to a socket and send it out over the wire when it is possible.

    The consequence of this internal buffering is that memory may grow. Users who experience large or growing bufferSize should attempt to "throttle" the data flows in their program with socket.pause() and socket.resume().

    Deprecated

    Since v14.6.0 - Use writableLength instead.

    Since

    v0.3.8

  2. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated
    Deprecated

Inherited from StObject

Inherited from Object

Inherited from Any

Inherited from AnyRef

Inherited from Any

Ungrouped