Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lanfei/websocket-lib

A lightweight WebSocket library for Node.
https://github.com/lanfei/websocket-lib

realtime websocket ws

Last synced: 7 days ago
JSON representation

A lightweight WebSocket library for Node.

Awesome Lists containing this project

README

        

# Node WebSocket Library

This is a lightweight WebSocket library for Node.

[![NPM](https://nodei.co/npm/websocket-lib.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/websocket-lib)

## Installation

```bash
$ npm install websocket-lib
```

### Documentation

- [API documentation](https://github.com/Lanfei/websocket-lib/blob/master/docs/API.md)
- [The WebSocket Protocol](https://tools.ietf.org/html/rfc6455)
- [The WebSocket API by W3C](https://www.w3.org/TR/websockets/)

## Examples

### Server

```js
var ws = require('websocket-lib');

var server = ws.createServer(function (session) {
console.log('client connected');

session.on('data', function (data) {
console.log('client msg:', data);
this.send('Hi, ' + data);
});

session.on('close', function () {
console.log('session closed');
});
});

server.listen(8000);
```

### Client

```js
var ws = require('websocket-lib');

var client = ws.connect('ws://localhost:8000', function (session) {

session.setEncoding('utf8');
session.send('Client');

session.on('data', function (data) {
console.log('server msg:', data);
});

session.on('close', function () {
console.log('session closed');
});
});
```

### Browser

```js
var ws = new WebSocket('ws://localhost:8000');
ws.onmessage = function (event) {
console.log('server msg:', event.data);
};
ws.onclose = function () {
console.log('session closed');
};
ws.onopen = function () {
ws.send('Client');
};
```