chrome.socket

Description: Use the chrome.socket module to send and receive data over the network using TCP and UDP connections.
Availability: Google Chrome 24
Permissions: "socket": ["rule1", "rule2"]
See Network Communications for rule syntax.
Learn more: Network Communications
Build Apps with Sencha ExtJS
Chrome Apps Office Hours: Networking APIs
Chrome Apps Office Hours: Controlling an AR Parrot Drone

chrome.socket reference

Types

SocketType

WriteInfo

Properties of WriteInfo

bytesWritten ( integer )
The number of bytes sent, or a negative error code.

NetworkInterface

Properties of NetworkInterface

name ( string )
The underlying name of the adapter. On *nix, this will typically be "eth0", "lo", etc.
address ( string )
The available IPv4/6 address.

Methods

create

chrome.socket.create(SocketType type, object options, function callback)

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

Parameters

type ( SocketType )
The type of socket to create. Must be tcp or udp.
options ( optional object )
The socket options.
callback ( function )
Called when the socket has been created.

Callback

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

function(object createInfo) {...};
createInfo ( object )
socketId ( integer )
The id of the newly created socket.

destroy

chrome.socket.destroy(integer socketId)

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

Parameters

socketId ( integer )
The socketId.

connect

chrome.socket.connect(integer socketId, string hostname, integer port, function callback)

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 ( integer )
The socketId.
hostname ( string )
The hostname or IP address of the remote machine.
port ( integer )
The port of the remote machine.
callback ( function )
Called when the connection attempt is complete.

Callback

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

function(integer result) {...};
result ( integer )

bind

chrome.socket.bind(integer socketId, string address, integer port, function callback)

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

Parameters

socketId ( integer )
The socketId.
address ( string )
The address of the local machine.
port ( integer )
The port of the local machine.
callback ( function )
Called when the bind attempt is complete.

Callback

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

function(integer result) {...};
result ( integer )

disconnect

chrome.socket.disconnect(integer socketId)

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

Parameters

socketId ( integer )
The socketId.

read

chrome.socket.read(integer socketId, integer bufferSize, function callback)

Reads data from the given connected socket.

Parameters

socketId ( integer )
The socketId.
bufferSize ( optional integer )
The read buffer size.
callback ( function )
Delivers data that was available to be read without blocking.

Callback

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

function(object readInfo) {...};
readInfo ( object )
resultCode ( integer )
The resultCode returned from the underlying read() call.
data ( arraybuffer )

write

chrome.socket.write(integer socketId, arraybuffer data, function callback)

Writes data on the given connected socket.

Parameters

socketId ( integer )
The socketId.
data ( arraybuffer )
The data to write.
callback ( function )
Called when the write operation completes without blocking or an error occurs.

Callback

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

function(WriteInfo writeInfo) {...};
writeInfo ( WriteInfo )

recvFrom

chrome.socket.recvFrom(integer socketId, integer bufferSize, function callback)

Receives data from the given UDP socket.

Parameters

socketId ( integer )
The socketId.
bufferSize ( optional integer )
The receive buffer size.
callback ( function )
Returns result of the recvFrom operation.

Callback

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

function(object recvFromInfo) {...};
recvFromInfo ( object )
resultCode ( integer )
The resultCode returned from the underlying recvfrom() call.
data ( arraybuffer )
address ( string )
The address of the remote machine.
port ( integer )

sendTo

chrome.socket.sendTo(integer socketId, arraybuffer data, string address, integer port, function callback)

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

Parameters

socketId ( integer )
The socketId.
data ( arraybuffer )
The data to write.
address ( string )
The address of the remote machine.
port ( integer )
The port of the remote machine.
callback ( function )
Called when the send operation completes without blocking or an error occurs.

Callback

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

function(WriteInfo writeInfo) {...};
writeInfo ( WriteInfo )

listen

chrome.socket.listen(integer socketId, string address, integer port, integer backlog, function callback)

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 ( integer )
The socketId.
address ( string )
The address of the local machine.
port ( integer )
The port of the local machine.
backlog ( optional integer )
Length of the socket's listen queue.
callback ( function )

Callback

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

function(integer result) {...};
result ( integer )

accept

chrome.socket.accept(integer socketId, function callback)

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 ( integer )
The socketId.
callback ( function )
The callback is invoked when a new socket is accepted.

Callback

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

function(object acceptInfo) {...};
acceptInfo ( object )
resultCode ( integer )
socketId ( optional integer )
The id of the accepted socket.

setKeepAlive

chrome.socket.setKeepAlive(integer socketId, boolean enable, integer delay, function callback)

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

Parameters

socketId ( integer )
The socketId.
enable ( boolean )
If true, enable keep-alive functionality.
delay ( optional integer )
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.

Callback

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

function(boolean result) {...};
result ( boolean )

setNoDelay

chrome.socket.setNoDelay(integer socketId, boolean noDelay, function callback)

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

Parameters

socketId ( integer )
The socketId.
noDelay ( boolean )
If true, disables Nagle's algorithm.
callback ( function )
Called when the setNoDelay attempt is complete.

Callback

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

function(boolean result) {...};
result ( boolean )

getInfo

chrome.socket.getInfo(integer socketId, function callback)

Retrieves the state of the given socket.

Parameters

socketId ( integer )
The socketId.
callback ( function )
Called when the state is available.

Callback

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

function(object result) {...};
result ( object )
socketType ( SocketType )
The type of the passed socket. This will be tcp or udp.
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.
peerAddress ( optional string )
If the underlying socket is connected, contains the IPv4/6 address of the peer.
peerPort ( optional integer )
If the underlying socket is connected, contains the port of the connected peer.
localAddress ( optional string )
If the underlying socket is bound or connected, contains its local IPv4/6 address.
localPort ( optional integer )
If the underlying socket is bound or connected, contains its local port.

getNetworkList

chrome.socket.getNetworkList(function callback)

Retrieves information about local adapters on this system.

Parameters

callback ( function )
Called when local adapter information is available.

Callback

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

function(array of NetworkInterface result) {...};
result ( array of NetworkInterface )

Sample Apps that use chrome.socket

  • Multicast Chat Demo App – Sending/Receiving multicast diagrams.
  • Parrot AR.Drone 2.0 Controller