0.0.2 • Published 1 year ago
@confis/synapse v0.0.2
Synapse: WebSocket Library for Effortless Communication
Synapse is a lightweight Node.js package that simplifies real-time, bidirectional communication between servers and clients using WebSockets. It provides a clean and intuitive API for both server-side and client-side development, making it easy to establish real-time connections and exchange data seamlessly.
Installation
Install Synapse using npm:
npm install @confis/synapse
Usage
Server-Side
- Import the
SynapseServer
class:
import { SynapseServer } from "synapse";
- Create a new
SynapseServer
instance:
const server = new SynapseServer();
- Listen for connection events:
server.on("connect", (socket) => {
console.log(`New socket connected! ID: ${socket.ID}`);
socket.send("hello", "world"); // Send welcome message
});
server.on("disconnect", (socket, code, reason) => {
console.log(`Socket ${socket.ID} disconnected`);
});
Client-Side
- Import the
SynapseClient
class:
import { SynapseClient } from "synapse";
- Create a new
SynapseClient
instance:
const client = new SynapseClient("ws://localhost:3000", {
reconnect: true, // Enable automatic reconnection
});
- Listen for connection and message events:
client.on("connect", () => {
console.log("Connected to server");
});
client.on("hello", (data) => {
console.log("Received message from server:", data);
});
Features
- Simple and Intuitive API: Synapse offers a clean and well-documented API for both server and client, making it easy to learn and integrate into your projects.
- Automatic Reconnection (Optional): The client can be configured to automatically reconnect to the server in case of disconnection, ensuring uninterrupted communication.
- Custom Event Handling: You can define custom events on both the server and client to exchange specific data types and handle them accordingly.