1.0.7 • Published 7 months ago

jconet-sockets v1.0.7

Weekly downloads
-
License
-
Repository
gitlab
Last release
7 months ago

JCoNet Sockets

Build Status coverage report Latest Release

Description

JCoNet Sockets is a custom WebSocket package inspired by Pusher, providing an easy-to-use interface for real-time communication in web applications. It includes:

  • Host: A standalone WebSocket server.
  • Server-Side Client: For triggering events from your server-side code.
  • Client-Side Client: For subscribing to channels and receiving events in the browser or Node.js applications.

Installation

Use your preferred package manager to install jconet-sockets:

# If you are using npm:
npm install jconet-sockets

# If you are using pnpm:
pnpm add jconet-sockets

# If you are using yarn:
yarn add jconet-sockets

# If you are using bun:
bun add jconet-sockets

Usage

Host

Set up the WebSocket host server. This can run independently on a server.

import { Host } from "jconet-sockets";

// Start the WebSocket host on port 8080
const host = new Host(8080);

Server-Side Client

Use the server-side client to trigger events from your backend application (e.g., in API routes or server processes).

import { ServerClient } from "jconet-sockets";

// Connect to the WebSocket host
const serverClient = new ServerClient("ws://localhost:8080");

// Trigger an event on a channel
serverClient.trigger("my-channel", "new-event", { message: "Hello World!" });

Client-Side Client

Use the client-side client in your frontend application to subscribe to channels and listen for events.

import { Client } from "jconet-sockets";

// Your JWT token for authentication
const token = "YOUR_JWT_TOKEN";

// Connect to the WebSocket host
const client = new Client("ws://localhost:8080", token);

// Subscribe to a channel
const channel = client.subscribe("my-channel");

// Listen for events on the channel
channel.on("new-event", (data) => {
  console.log("Received event data:", data);
});

// Optionally, trigger events from the client
channel.trigger("client-event", { clientData: "Hello from client!" });

Generating JWT Tokens

On your server, generate JWT tokens for authenticating clients. This ensures secure communication and channel authorization.

import { generateToken } from "jconet-sockets";

// Generate a token for a user with access to specific channels
const token = generateToken("user-id", ["my-channel", "another-channel"]);

Development

If you want to contribute or run the package locally:

  1. Clone the repository:

    git clone https://gitlab.com/jconet-ltd/jconet-socket.git
  2. Install dependencies:

    pnpm install
  3. Run tests:

    pnpm test
  4. Build the package:

    pnpm build
  5. Run ESLint:

    pnpm lint

Contributing

Contributions are welcome and greatly appreciated! Please open an issue or submit a pull request on GitLab.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

For questions or support, please contact Our Project Support Email.

1.0.7

7 months ago

1.0.6

7 months ago

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago