@confis/synapse
v0.0.2
Published
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 establi
Readme
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/synapseUsage
Server-Side
- Import the
SynapseServerclass:
import { SynapseServer } from "synapse";- Create a new
SynapseServerinstance:
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
SynapseClientclass:
import { SynapseClient } from "synapse";- Create a new
SynapseClientinstance:
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.
