easy and secure websocket communication
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
@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.
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.
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...');
});
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');
@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();
});
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');
@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.
@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.
To scale your WebSocket services, you can utilize load balancers and ensure your @push.rocks/smartsocket instances are stateless to allow for horizontal scaling.
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.
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.
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.