Provides easy and secure websocket communication mechanisms, including server and client implementation, function call routing, connection management, and tagging.
Go to file
2024-04-14 18:20:36 +02:00
.gitea/workflows fix(core): update 2023-07-21 03:53:41 +02:00
.vscode fix(core): update 2022-12-28 13:52:16 +01:00
test fix(core): update 2023-09-09 23:32:12 +02:00
ts fix(core): update 2023-09-10 10:41:41 +02:00
.gitignore fix(core): update 2020-09-24 18:04:11 +00:00
npmextra.json update tsconfig 2024-04-14 18:20:36 +02:00
package.json update tsconfig 2024-04-14 18:20:36 +02:00
pnpm-lock.yaml fix(core): update 2023-09-09 23:32:12 +02:00
readme.hints.md update tsconfig 2024-04-14 18:20:36 +02:00
readme.md update tsconfig 2024-04-14 18:20:36 +02:00
tsconfig.json update tsconfig 2024-04-01 21:41:07 +02:00

@push.rocks/smartsocket

easy and secure websocket communication

Install

To install @push.rocks/smartsocket, you can use npm or yarn as follows:

npm install @push.rocks/smartsocket --save

or

yarn add @push.rocks/smartsocket

Usage

@push.rocks/smartsocket offers a robust solution for easy and secure WebSocket communication, utilizing Typescript for clean and maintainable code. Below are comprehensive examples covering various scenarios and features provided by the module.

Getting Started

First, ensure you've installed the module as shown in the "Install" section. Once installed, you can start using @push.rocks/smartsocket in your project.

Setting Up a WebSocket Server

To create a WebSocket server that clients can connect to:

import { Smartsocket } from '@push.rocks/smartsocket';

// Create a new instance of Smartsocket for the server.
const server = new Smartsocket({ alias: 'myServer' });

// Define a SocketFunction that clients can call
server.addSocketFunction({
  funcName: 'greet',
  funcDef: async (data) => {
    console.log(`Server received: ${data.message}`);
    return { reply: `Hello, ${data.name}!` };
  }
});

// Start the Smartsocket server
server.start().then(() => {
  console.log('WebSocket server is running...');
});

Creating a WebSocket Client

Create a client that connects to the WebSocket server and interacts with it:

import { SmartsocketClient } from '@push.rocks/smartsocket';

// Create a SmartsocketClient instance and connect to the server
const client = new SmartsocketClient({
  url: 'ws://localhost',
  port: 3000,
  alias: 'myClient'
});

client.connect().then(() => {
  console.log('Connected to WebSocket server');
});

// Define a function to call the server's 'greet' function
async function greetServer(name) {
  const response = await client.serverCall('greet', { name: name, message: 'Hello!' });
  console.log(`Server replied: ${response.reply}`);
}

// Use the function
greetServer('Alice');

Handling Disconnections and Reconnections

@push.rocks/smartsocket provides mechanisms to handle client disconnections and attempt reconnections:

client.on('disconnect', () => {
  console.log('Disconnected from server. Attempting to reconnect...');
  client.connect();
});

Sending Binary Data

The library supports the transmission of binary data efficiently:

import fs from 'fs';

// Function to send a binary file to the server
async function sendBinaryData(filePath) {
  const fileBuffer = fs.readFileSync(filePath);
  await client.serverCall('sendFile', { file: fileBuffer });
}

sendBinaryData('./path/to/your/file.png');

Securing Your WebSocket Communication

@push.rocks/smartsocket leverages secure WebSocket (WSS) connections to ensure that data transferred between the client and server is encrypted. When setting up your Smartsocket server or client, use wss:// in your URL to enable secure communication.

Advanced Usage

Mesh Networking

@push.rocks/smartsocket allows for the creation of complex mesh network configurations, enabling servers to communicate with other servers, forming a robust network with multiple nodes.

Scaling with @push.rocks/smartsocket

To scale your WebSocket services, you can utilize load balancers and ensure your @push.rocks/smartsocket instances are stateless to allow for horizontal scaling.

Conclusion

This guide has covered how to set up basic WebSocket communication with @push.rocks/smartsocket, handle disconnections/reconnections, secure your communication, send binary data, and briefly touched on advanced concepts like mesh networking and scaling.

For more detailed documentation, visit the official @push.rocks/smartsocket GitLab repository.

Remember, WebSocket communication with @push.rocks/smartsocket is not only about sending and receiving messages. It's about creating a fast, reliable, and secure communication channel for your real-time applications.

Happy coding!


Please note, the documentation above is a starting point. Depending on the complexity and requirements of your application, you may need to explore more features and configurations provided by @push.rocks/smartsocket. Always refer to the official documentation for the most current information and best practices.

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.