Welcome What's new in Chrome extensions Getting started API Reference Samples
Welcome What's new in Chrome extensions Getting started API Reference Samples

chrome.socket

This API is part of the deprecated Chrome Apps platform. Learn more about migrating your app.
  • Description

    Use the chrome.socket API to send and receive data over the network using TCP and UDP connections. Note: Starting with Chrome 33, this API is deprecated in favor of the sockets.udp, sockets.tcp and sockets.tcpServer APIs.

  • Permissions
    socket

Summary

Types

AcceptInfo

Properties

  • resultCode
    number
  • socketId
    number optional

    The id of the accepted socket.

CreateInfo

Properties

  • socketId
    number

    The id of the newly created socket.

CreateOptions

NetworkInterface

Properties

  • address
    string

    The available IPv4/6 address.

  • name
    string

    The underlying name of the adapter. On *nix, this will typically be "eth0", "lo", etc.

  • prefixLength
    number

    The prefix length

ReadInfo

Properties

  • data
    ArrayBuffer
  • resultCode
    number

    The resultCode returned from the underlying read() call.

RecvFromInfo

Properties

  • address
    string

    The address of the remote machine.

  • data
    ArrayBuffer
  • port
    number
  • resultCode
    number

    The resultCode returned from the underlying recvfrom() call.

SecureOptions

Properties

SocketInfo

Properties

  • connected
    boolean

    Whether or not the underlying socket is connected.

    For tcp sockets, this will remain true even if the remote peer has disconnected. Reading or writing to the socket may then result in an error, hinting that this socket should be disconnected via disconnect().

    For udp sockets, this just represents whether a default remote address has been specified for reading and writing packets.

  • localAddress
    string optional

    If the underlying socket is bound or connected, contains its local IPv4/6 address.

  • localPort
    number optional

    If the underlying socket is bound or connected, contains its local port.

  • peerAddress
    string optional

    If the underlying socket is connected, contains the IPv4/6 address of the peer.

  • peerPort
    number optional

    If the underlying socket is connected, contains the port of the connected peer.

  • socketType

    The type of the passed socket. This will be tcp or udp.

TLSVersionConstraints

Properties

  • max
    string optional
  • min
    string optional

    The minimum and maximum acceptable versions of TLS. These will be tls1, tls1.1, tls1.2, or tls1.3.

WriteInfo

Properties

  • bytesWritten
    number

    The number of bytes sent, or a negative error code.

SocketType

Enum

"tcp", or "udp"

Methods

accept

socket.accept(socketId: number, callback: function)

This method applies to TCP sockets only. Registers a callback function to be called when a connection is accepted on this listening server socket. Listen must be called first. If there is already an active accept callback, this callback will be invoked immediately with an error as the resultCode.

Parameters

  • socketId
    number

    The socketId.

  • callback
    function

    The callback is invoked when a new socket is accepted.

    The callback parameter should be a function that looks like this:

    (acceptInfo: AcceptInfo) => {...}

bind

socket.bind(socketId: number, address: string, port: number, callback: function)

Binds the local address for socket. Currently, it does not support TCP socket.

Parameters

  • socketId
    number

    The socketId.

  • address
    string

    The address of the local machine.

  • port
    number

    The port of the local machine.

  • callback
    function

    Called when the bind attempt is complete.

    The callback parameter should be a function that looks like this:

    (result: number) => {...}
    • result
      number

connect

socket.connect(socketId: number, hostname: string, port: number, callback: function)

Connects the socket to the remote machine (for a tcp socket). For a udp socket, this sets the default address which packets are sent to and read from for read() and write() calls.

Parameters

  • socketId
    number

    The socketId.

  • hostname
    string

    The hostname or IP address of the remote machine.

  • port
    number

    The port of the remote machine.

  • callback
    function

    Called when the connection attempt is complete.

    The callback parameter should be a function that looks like this:

    (result: number) => {...}
    • result
      number

create

socket.create(type: SocketType, options?: CreateOptions, callback: function)

Creates a socket of the specified type that will connect to the specified remote machine.

Parameters

  • The type of socket to create. Must be tcp or udp.

  • options
    CreateOptions optional

    The socket options.

  • callback
    function

    Called when the socket has been created.

    The callback parameter should be a function that looks like this:

    (createInfo: CreateInfo) => {...}

destroy

socket.destroy(socketId: number)

Destroys the socket. Each socket created should be destroyed after use.

Parameters

  • socketId
    number

    The socketId.

disconnect

socket.disconnect(socketId: number)

Disconnects the socket. For UDP sockets, disconnect is a non-operation but is safe to call.

Parameters

  • socketId
    number

    The socketId.

getInfo

socket.getInfo(socketId: number, callback: function)

Retrieves the state of the given socket.

Parameters

  • socketId
    number

    The socketId.

  • callback
    function

    Called when the state is available.

    The callback parameter should be a function that looks like this:

    (result: SocketInfo) => {...}

getJoinedGroups

socket.getJoinedGroups(socketId: number, callback: function)

Get the multicast group addresses the socket is currently joined to.

Parameters

  • socketId
    number

    The socketId.

  • callback
    function

    Called with an array of strings of the result.

    The callback parameter should be a function that looks like this:

    (groups: string[]) => {...}
    • groups
      string[]

getNetworkList

socket.getNetworkList(callback: function)

Retrieves information about local adapters on this system.

Parameters

  • callback
    function

    Called when local adapter information is available.

    The callback parameter should be a function that looks like this:

    (result: NetworkInterface[]) => {...}

joinGroup

socket.joinGroup(socketId: number, address: string, callback: function)

Join the multicast group and start to receive packets from that group. The socket must be of UDP type and must be bound to a local port before calling this method.

Parameters

  • socketId
    number

    The socketId.

  • address
    string

    The group address to join. Domain names are not supported.

  • callback
    function

    Called when the join group operation is done with an integer parameter indicating the platform-independent error code.

    The callback parameter should be a function that looks like this:

    (result: number) => {...}
    • result
      number

leaveGroup

socket.leaveGroup(socketId: number, address: string, callback: function)

Leave the multicast group previously joined using joinGroup. It's not necessary to leave the multicast group before destroying the socket or exiting. This is automatically called by the OS.

Leaving the group will prevent the router from sending multicast datagrams to the local host, presuming no other process on the host is still joined to the group.

Parameters

  • socketId
    number

    The socketId.

  • address
    string

    The group address to leave. Domain names are not supported.

  • callback
    function

    Called when the leave group operation is done with an integer parameter indicating the platform-independent error code.

    The callback parameter should be a function that looks like this:

    (result: number) => {...}
    • result
      number

listen

socket.listen(socketId: number, address: string, port: number, backlog?: number, callback: function)

This method applies to TCP sockets only. Listens for connections on the specified port and address. This effectively makes this a server socket, and client socket functions (connect, read, write) can no longer be used on this socket.

Parameters

  • socketId
    number

    The socketId.

  • address
    string

    The address of the local machine.

  • port
    number

    The port of the local machine.

  • backlog
    number optional

    Length of the socket's listen queue.

  • callback
    function

    Called when listen operation completes.

    The callback parameter should be a function that looks like this:

    (result: number) => {...}
    • result
      number

read

socket.read(socketId: number, bufferSize?: number, callback: function)

Reads data from the given connected socket.

Parameters

  • socketId
    number

    The socketId.

  • bufferSize
    number optional

    The read buffer size.

  • callback
    function

    Delivers data that was available to be read without blocking.

    The callback parameter should be a function that looks like this:

    (readInfo: ReadInfo) => {...}

recvFrom

socket.recvFrom(socketId: number, bufferSize?: number, callback: function)

Receives data from the given UDP socket.

Parameters

  • socketId
    number

    The socketId.

  • bufferSize
    number optional

    The receive buffer size.

  • callback
    function

    Returns result of the recvFrom operation.

    The callback parameter should be a function that looks like this:

    (recvFromInfo: RecvFromInfo) => {...}

secure

socket.secure(socketId: number, options?: SecureOptions, callback: function)

Start a TLS client connection over a connected TCP client socket.

Parameters

  • socketId
    number

    The connected socket to use.

  • options
    SecureOptions optional

    Constraints and parameters for the TLS connection.

  • callback
    function

    Called when the TLS connection attempt is complete.

    The callback parameter should be a function that looks like this:

    (result: number) => {...}
    • result
      number

sendTo

socket.sendTo(socketId: number, data: ArrayBuffer, address: string, port: number, callback: function)

Sends data on the given UDP socket to the given address and port.

Parameters

  • socketId
    number

    The socketId.

  • data
    ArrayBuffer

    The data to write.

  • address
    string

    The address of the remote machine.

  • port
    number

    The port of the remote machine.

  • callback
    function

    Called when the send operation completes without blocking or an error occurs.

    The callback parameter should be a function that looks like this:

    (writeInfo: WriteInfo) => {...}

setKeepAlive

socket.setKeepAlive(socketId: number, enable: boolean, delay?: number, callback: function)

Enables or disables the keep-alive functionality for a TCP connection.

Parameters

  • socketId
    number

    The socketId.

  • enable
    boolean

    If true, enable keep-alive functionality.

  • delay
    number optional

    Set the delay seconds between the last data packet received and the first keepalive probe. Default is 0.

  • callback
    function

    Called when the setKeepAlive attempt is complete.

    The callback parameter should be a function that looks like this:

    (result: boolean) => {...}
    • result
      boolean

setMulticastLoopbackMode

socket.setMulticastLoopbackMode(socketId: number, enabled: boolean, callback: function)

Set whether multicast packets sent from the host to the multicast group will be looped back to the host.

Note: the behavior of setMulticastLoopbackMode is slightly different between Windows and Unix-like systems. The inconsistency happens only when there is more than one application on the same host joined to the same multicast group while having different settings on multicast loopback mode. On Windows, the applications with loopback off will not RECEIVE the loopback packets; while on Unix-like systems, the applications with loopback off will not SEND the loopback packets to other applications on the same host. See MSDN: http://goo.gl/6vqbj

Calling this method does not require multicast permissions.

Parameters

  • socketId
    number

    The socketId.

  • enabled
    boolean

    Indicate whether to enable loopback mode.

  • callback
    function

    Called when the configuration operation is done.

    The callback parameter should be a function that looks like this:

    (result: number) => {...}
    • result
      number

setMulticastTimeToLive

socket.setMulticastTimeToLive(socketId: number, ttl: number, callback: function)

Set the time-to-live of multicast packets sent to the multicast group.

Calling this method does not require multicast permissions.

Parameters

  • socketId
    number

    The socketId.

  • ttl
    number

    The time-to-live value.

  • callback
    function

    Called when the configuration operation is done.

    The callback parameter should be a function that looks like this:

    (result: number) => {...}
    • result
      number

setNoDelay

socket.setNoDelay(socketId: number, noDelay: boolean, callback: function)

Sets or clears TCP_NODELAY for a TCP connection. Nagle's algorithm will be disabled when TCP_NODELAY is set.

Parameters

  • socketId
    number

    The socketId.

  • noDelay
    boolean

    If true, disables Nagle's algorithm.

  • callback
    function

    Called when the setNoDelay attempt is complete.

    The callback parameter should be a function that looks like this:

    (result: boolean) => {...}
    • result
      boolean

write

socket.write(socketId: number, data: ArrayBuffer, callback: function)

Writes data on the given connected socket.

Parameters

  • socketId
    number

    The socketId.

  • data
    ArrayBuffer

    The data to write.

  • callback
    function

    Called when the write operation completes without blocking or an error occurs.

    The callback parameter should be a function that looks like this:

    (writeInfo: WriteInfo) => {...}
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.