0.0.2 • Published 1 year ago

@confis/synapse v0.0.2

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

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

  1. Import the SynapseServer class:
import { SynapseServer } from "synapse";
  1. Create a new SynapseServer instance:
const server = new SynapseServer();
  1. 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

  1. Import the SynapseClient class:
import { SynapseClient } from "synapse";
  1. Create a new SynapseClient instance:
const client = new SynapseClient("ws://localhost:3000", {
  reconnect: true, // Enable automatic reconnection
});
  1. 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.