Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

websockets

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

websockets

WebSocket Server & Client API

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

node-websockets

Web Socket Server and Client API
Support Protocols
  • RFC 6455
  • draft-ietf-hybi-thewebsocketprotocol-10
  • draft-ietf-hybi-thewebsocketprotocol-00

Install

npm install websockets

Usage

require websockets

var websockets = require("websockets");

Server:

Server is a wrapper of http/https server.


// http based server
var server = websockets.createServer();
server.on('connect', function(socket) {
  socket.on('message', function(message) {
    socket.send('echo a message:' + message);
    ......
  });
}).listen(80);

// https based server
var secure = websockets.createServer({
  key: ssl_key,
  cert: ssl_cert
});
secure.on('connect', function(socket) {
  ......
}).listen(443);


Extended Servers such as express are also available.

// In case of 'express'
var express = require('express');

var svr = express.createServer();
svr.get('/', function(req, res) {
  ......
});
svr.configure(function() {
  ......
});

var server = websockets.createServer({
  server: svr
});
server.on('connect', function(socket) {
  socket.on('message', function(message) {
    socket.send('echo a message:' + message);
    ......
  });
}).listen(80);

Client:

Client has the interfaces like html5 WebSocket.

var socket = new websockets.WebSocket('wss://127.0.0.1');
socket.on('open', function() {
  socket.send('a message');
  ......
});

APIs

websockets.Server


Event: 'connect'

function (socket) {}

Emitted when client-server opening handshake has succeeded. socket is an instance of WebSocket.


server.broadcast(string)

Not Implemented. Sends string to all clients connected with server.


server.broadcast(buffer)

Not Implemented. Sends binary data(buffer) to all clients connected with server.


websockets.WebSocket


Event: 'open'

function () {}

Emitted when a client-server connection is successfully established.


Event: 'message'

function (data) {}

Emitted when the socket has received a message. The type of data is either string(string data) or Buffer(binary data).


Event: 'error'

function (exception) {}

Emitted on error. exception is an instance of Error.


Event: 'close'

function () {}

Emitted when a client-server connection has closed.


socket.send(string)

Sends string to the other endpoint.


socket.send(buffer)

Sends binary data(buffer) to the other endpoint.


socket.close()

Sends a connection close request to the other endpoint.


TODO = * implementation of server broadcast

FAQs

Package last updated on 17 May 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc