websocket
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
bufferutil | 4.0.8 | 158.33 kB | MIT | prod | 1 |
debug | 2.6.9 | 16.13 kB | MIT | prod | |
es5-ext | 0.10.64 | 365.2 kB | ISC | prod | 1 |
typedarray-to-buffer | 3.1.5 | 3.67 kB | MIT | prod | |
utf-8-validate | 5.0.10 | 155.32 kB | MIT | prod | 1 |
yaeti | 0.0.6 | 3.34 kB | MIT | prod |
The package "websocket" is a client and server library that implements the WebSocket protocol as per RFC 6455 specification. This allows real-time communication between a client and a server. The JavaScript implementation is designed to work with Node.js and supports the WebSocket protocol versions 8 and 13. It's capable of handling and aggregating received fragmented messages, fragmenting outgoing messages, tuning settings and more. It also supports a W3C WebSocket API for applications running on both Node and browsers.
To use the "websocket" package, you first need to install it using npm with the command npm install websocket
executed in your project root. You can then require it in your code using var WebSocketServer = require('websocket').server;
.
Here's an example of how to set up a simple WebSocket server:
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
server.listen(8080, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});
wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
});
wsServer.on('request', function(request) {
var connection = request.accept('echo-protocol', request.origin);
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
connection.sendUTF(message.utf8Data);
}
});
connection.on('close', function(reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});
This code creates an HTTP server and associates a new WebSocketServer with it, listening on port 8080.
The detailed API documentation for the "websocket" package can be found in the docs folder on the GitHub repository. The link is here: git+https://github.com/theturtle32/WebSocket-Node/docs/index.md. It provides comprehensive information about the WebSocket Client, Server Implementation for Node with various examples and use-cases.