This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

This API is available on Firefox OS for privileged or certified applications only.


Provides access to a raw TCP socket API in JavaScript.

The main entry point for this API is the navigator.mozTCPSocket property which is a TCPSocket object.

Note: Only certified apps can accept incoming connections on a port below 1024.

API overview

interface TCPSocket{
  readonly attribute DOMString host;
  readonly attribute unsigned short port;
  readonly attribute boolean ssl;
  readonly attribute unsigned long bufferedAmount;
  readonly attribute DOMString binaryType;
  readonly attribute DOMString readyState;

  TCPSocket open(DOMString host, unsigned short port, [object options]);
  TCPServerSocket listen(unsigned short port, [object options, [unsigned short backlog]])
  void upgradeToSecure();
  void suspend();
  void resume();
  void close();
  boolean send(in jsval data);

  attribute onopen;
  attribute ondrain;
  attribute ondata;
  attribute onerror;
  attribute onclose;

Properties Read only
A string representing the host name of the server the socket is connected to.
TCPSocket.port Read only
A number representing the port the socket is connected to.
TCPSocket.ssl Read only
A boolean indicating whether the socket is encrypted with SSL (true) or not (false).
TCPSocket.bufferedAmount Read only
The number of bytes of not-yet-sent data in the socket buffered.
TCPSocket.binaryType Read only
The type of data used. Possible value is arraybuffer, or string.
TCPSocket.readyState Read only
The socket state. Possible value is connecting, open, closing, or closed.

Events handler

A handler for the open event. After this event, the socket is ready to send and receive data.
A handler for the drain event. This event is triggered each time the buffer of data is flushed.
A handler for the error event.
A handler for the data event. This event is triggered each time data has been received.
A handler for the close event.


Closes the connection., port [, options])
Returns a new TCPSocket object connected to the given host at the given port.
TCPSocket.listen(port [, options [, backlog]]) Requires FirefoxOS 1.2
Returns a new TCPServerSocket object listening the given port.
Resumes the data events.
Buffers data to be sent across the network.
Pauses the data events.
TCPSocket.upgradeToSecure() Requires FirefoxOS 1.2
Enable secure on channel.


Not part of any specification yet; however, this API is discussed at W3C as part of the System Applications Working Group under the TCP and UDP Socket API (formerly known as the Raw Sockets API) proposal.

See also

Document Tags and Contributors

 Last updated by: Sheppy,