socketit
v2.1.1
Published
Socketit: A simple and powerful toolkit for WebSocket communication.
Downloads
116
Maintainers
Readme
Socketit
Socketit: A simple and powerful toolkit for WebSocket communication.
Features
- Supports request-response and publish-subscribe patterns with
async
handlers. - Built-in timeout handling for requests.
- Customizable routes for handling WebSocket messages.
- Automatic reconnection for clients.
- Built-in TLS support with options for self-signed certificates.
- Compression with
permessage-deflate
. - Ping mechanism to ensure connection health.
- Works with Node.js.
Installation
npm install socketit
Usage
Server Example
const { Server } = require('socketit');
const server = new Server({
port: 8888,
routes: {
echo: async (data) => data, // Async handler to echo back data
reverse: async (data) => data.split('').reverse().join(''), // Reverse a string
},
});
server.on('connection', (channel) => {
console.log('New client connected');
channel.on('disconnected', () => {
console.log('Client disconnected');
});
});
server.start().then(() => {
console.log('Server is running on port 8443 with TLS');
});
Client Example
const { Client } = require('socketit');
const client = new Client('wss://localhost:8443', {
rejectUnauthorized: false, // Allow self-signed certificates
routes: {
greeting: async (data) => {
console.log('Server says:', data);
},
},
});
client.on('connected', async (channel) => {
console.log('Connected to server');
try {
const response = await channel.request('echo', { message: 'Hello, server!' });
console.log('Server responded:', response);
const reversed = await channel.request('reverse', 'Hello');
console.log('Reversed string:', reversed);
} catch (err) {
console.error('Request failed:', err.message);
}
});
client.on('disconnected', () => {
console.log('Disconnected from server');
});
API
Server
Constructor
new Server(options)
Options:
port
(number): The port for the WebSocket server. Default is8080
.externalServer
(http/https server instance): The server to use. If you need TLS, pass an https serverroutes
(object): An object defining methods to handle incoming messages. For example:{ echo: async (data) => data }
perMessageDeflate
(boolean): Enable WebSocket compression. Default isfalse
.
Methods
.start()
: Starts the WebSocket server. Returns aPromise
that resolves when the server is ready..stop()
: Stops the server. Returns aPromise
that resolves when the server has stopped.
Client
Constructor
new Client(url, options)
Parameters:
url
(string): The WebSocket server URL.options
(object): Configuration options.autoReconnect
(boolean): Automatically reconnect on disconnection. Default istrue
.rejectUnauthorized
(boolean): Allow self-signed certificates. Default istrue
.perMessageDeflate
(boolean): Enable WebSocket compression. Default isfalse
.routes
(object): An object defining methods to handle incoming messages.
Methods
.close()
: Closes the WebSocket connection.
Channel
Methods
.request(method, data, options)
- Sends a request to the server and waits for a response.
- Supports async handlers.
- Parameters:
method
(string): The name of the method to call.data
(any): Data to send with the request.options
(object): Additional options for the request.timeout
(number): Timeout for the request in milliseconds.
.publish(method, data)
- Sends a message to the server without expecting a response.
- Parameters:
method
(string): The name of the method to call.data
(any): Data to send.
.on(event, listener)
- Listens for events (
connected
,disconnected
, etc.). - Parameters:
event
(string): The event name.listener
(function): The callback function.
- Listens for events (
License
This project is licensed under the MIT License.
Made with ❤️ by Kedem