Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/lanfei/websocket-lib
- Owner: Lanfei
- License: mit
- Created: 2015-07-24T11:32:32.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-10-25T15:37:33.000Z (over 2 years ago)
- Last Synced: 2024-04-12T17:33:11.968Z (10 months ago)
- Topics: realtime, websocket, ws
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/websocket-lib
- Size: 542 KB
- Stars: 4
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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');
};
```