@fillipe143/node-socket

1.0.1 • Public • Published

NodeSocket

NodeSocket is a library written in Node.js that aims to create WebSocket connections between servers and clients.

Read the docs here

Examples

Look at the examples of server usage in server.js and client usage in client.js. The scripts cover the most common use cases.

Install

Create an NPM project and write to the terminal: npm install @fillipe143/node-socket

Then import the library using

To server:

const { ServerSocket } = require('@fillipe143/node-socket');

To client:

const { ClientSocket } = require('@fillipe143/node-socket');

Create Server

const { ServerSocket } = require('@fillipe143/node-socket');

//http://localhost:3000
const options = {
    host: 'localhost',
    port: 3000
}

const server = new ServerSocket(options);

Connect to Server

const { ClientSocket } = require('@fillipe143/node-socket');

//http://localhost:3000
const options = {
    host: 'localhost',
    port: 3000
}

const client = new ClientSocket(options);

Handling Events

on('connect', () => void): this //When the connection is created
on('error', (err) => void): this //When the connection fails
on('data', (data) => void): this //Receive data from other WebSocket libraries
on('close', () => void): this //When the connection is terminated

Send Data

const { ServerSocket } = require('@fillipe143/node-socket');

const options = {
    host: 'localhost',
    port: 3000
}

const server = new ServerSocket(options);

server.on('connect', () => {
    server.emit('event name', 'event data');
});

Receive Data

const { ClientSocket } = require('@fillipe143/node-socket');

const options = {
    host: 'localhost',
    port: 3000
}

const client = new ClientSocket(options);

server.on('event name', data => {
    console.log('Received: ' + data);
});

Note

Repository: https://github.com/Fillipe143/NodeSocket

Version: 1.0.1

Readme

Keywords

Package Sidebar

Install

npm i @fillipe143/node-socket

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

5.81 kB

Total Files

5

Last publish

Collaborators

  • fillipe143