The following chart shows the availability of each WebSocket module API function on each platform.
Linux (Ubuntu) |
Tizen (Raspberry Pi) |
Raspbian (Raspberry Pi) |
Nuttx (STM32F4-Discovery) |
TizenRT (Artik053) |
|
---|---|---|---|---|---|
websocket.connect | O | O | O | X | O |
websocket.close | O | O | O | X | O |
websocket.ping | O | O | O | X | O |
websocket.send | O | O | O | X | O |
WebSocket provides full-duplex communication over a TCP connection. It is designed to work over HTTP ports 80 and 443.
WebSocket requires you to enable both the TLS
and the WebSocket
module. This can be done by compiling IoT.js with --cmake-param=-DENABLE_MODULE_WEBSOCKET=ON
. Currently WebSocket only works if TLS is enabled as well.
Create a new Websocket server instance.
Create a new server instance. One of port
or server
must be provided or an error is thrown. An HTTP server is automatically created, started, and used if port
is set. If secure
is set TLS server is automatically created, started and used. The tls
module is required or an error is thrown. To use an external HTTP/S server instead, specify only server
. In this case the HTTP/S server must be started manually.
options
{Object}port
{Number}host
{String} Optional. Defaults tolocalhost
.server
{Object} Optional.path
{String} Optional. Defaults to/
.secure
{Boolean} Optional.key
{String} Optional. (Required onsecure
server)cert
{String} Optional. (Required onsecure
server)
callback
{Function} Optional. The function which will be executed when the client successfully connected to the server.
Emits a connection
event when the connection is established.
Example
var websocket = require('websocket');
var options = {
port: 9999
}
var server = new websocket.Server(options, Listener);
function Listener(ws) {
console.log('Client connected: handshake done!');
ws.on('message', function (msg) {
console.log('Message received: %s', msg.toString());
ws.send(msg.toString(), {mask: true, binary: false}); //echo
server.close();
});
ws.on('ping', function (msg) {
console.log('Ping received: %s', msg.toString());
});
ws.on('close', function (msg) {
console.log('Client close :\n'
'Reason: ' + msg.reason + ' Code: ' + msg.code);
});
ws.on('error', function (msg) {
console.log('Error: %s', msg.toString());
});
server.broadcast('Message to all clients', {mask: false, binary: false});
};
server.on('error', function (msg) {
console.log('Error: %s', msg.toString());
});
server.on('close', function (msg) {
console.log('Server close: \nReason: ' +
msg.reason + ' Code: ' + msg.code);
});
Example using http server
var websocket = require('websocket');
var http = require('http');
var httpserver = http.createServer().listen(9999);
options = {
server: httpserver
};
var wss3 = new websocket.Server(options, Listener);
function Listener(ws) {
console.log('Client connected: handshake done!');
};
Returns an object with port
, family
, and address
properties specifying
the bound address, the family name, and port of the server.
Example
var websocket = require('websocket');
var options = {
port: 9999
}
var server = new websocket.Server(options, function(ws) {
});
console.log(server.address());
You can specify a close message and a close code as well. More info on them can be read here: https://tools.ietf.org/html/rfc6455#section-7.4.1
reason
{String} Optional. Defaults toConnection successfully closed
.code
{Number} Optional. Defaults to1000
.
Close the Websocket server, terminate all clients and emit the close
event.
Example
var websocket = require('websocket');
var options = {
port: 9999
}
var server = new websocket.Server(options, Listener);
function Listener(ws) {
console.log('Client connected: handshake done!');
server.close('Connection successfully closed', 1000);
};
You can specify a message that will be sent to every clients.
The mask
will specify whether the data frame should be masked or not.
The binary
will specify that if the data frame mode should be text or binary, default to text.
More info on them can be read here: https://tools.ietf.org/html/rfc6455#section-5.6
message
{String}options
{Object} Optional.mask
{Boolean} Optional. Defaults totrue
.binary
{Boolean} Optional. Defaults tofalse
.
Send message to all clients.
Example
var websocket = require('websocket');
var options = {
port: 9999
}
var server = new websocket.Server(options, Listener);
function Listener(ws) {
console.log('Client connected: handshake done!');
};
server.broadcast('Message to receive all client',
{mask: true, binary: false});
socket
{Websocket}
Emitted when the handshake is complete.
message
{Object}reason
{String}code
{Number}
Emitted when the server close.
error
{Error}
Emmitted when an error occurs on the server.
The Websocket
client can simultaneously receive and send data. Both net
and TLS
sockets are supported, however the latter is recommended, since websocket
itself doesn't provide a secure enough context to communicate sensitive data.
Connects to a websocket
server, host names can be prefixed with ws://
or wss://
.
host
{string} Optional. Defaults tolocalhost
.port
{number} Optional. Defaults to80
if thehost
begins withws://
or443
withwss://
.path
{Buffer | string} Optional. Defaults to/
. If given, the client connects to thatendpoint
.callback
{function} Optional. The function which will be executed when the client successfully connected to the server.
Emits an open
event when the connection is established.
Example
var ws = require('websocket');
var my_websocket = new ws.Websocket();
my_websocket.connect('wss://127.0.0.1', 443, '/my_endpoint', function() {
my_websocket.close('Successful connection', 1000);
});
Closes the websocket
connection with the server. You can specify a close message
and a close code
as well. More info on them can be read here: https://tools.ietf.org/html/rfc6455#section-7.4.1
message
{Buffer | string} Optional. Thismessage
is sent to the server as a close message, mostly for explaining the statuscode
.code
{number} Optional. Thecode
indicates the reason why thewebsocket
connection was closed. Defaults to 1000.callback
{function} Optional. The function will be executed when thewebsocket
connection is closed.
Emits a close
event when the connection is closed.
Sends a ping
to the server. If there is no response in the next 30 seconds, the connection is closed.
message
{Buffer | string} Optional. Themessage
is used to identify theping
frame.mask
{boolean} Optional. Defaults tofalse
. Sets to mask themessage
or not.callback
{function} Optional. The function to be executed when the server sends a response to theping
.
Example
my_websocket.ping('Ping frame 1', true, function(msg) {
console.log('Pong frame successfully received for frame ' + msg);
});
Sends data to the server. It can be either binary
or utf8
data.
message
{Buffer | string} Themessage
to be sent to the server.options
{Object}mask
{boolean} Optional. Defaults tofalse
. If set, themessage
is masked.binary
{boolean} Optional. Defaults tofalse
. If set, themessage
is expected to be binary data.
callback
{function} Optional. The function to be executed when theframe
is successfully sent.
Example
my_websocket.send('My first WebSocket message!', {mask: true, binary: false}, function() {
console.log('The data was successfully written to the socket!');
});
Having closed the websocket
connection, with the server, a close
is emitted.
If an error
occurs, the error
event is emitted, with the corresponding error message.
The message
event is emitted when the client receives data from the server.
Emitted when the client established a websocket
connection with the server.