smartsocket/readme.md

150 lines
5.7 KiB
Markdown
Raw Normal View History

2023-07-21 01:53:41 +00:00
# @push.rocks/smartsocket
2020-09-29 19:43:10 +00:00
easy and secure websocket communication
2024-04-14 16:20:36 +00:00
## Install
To install @push.rocks/smartsocket, you can use npm or yarn as follows:
```shell
npm install @push.rocks/smartsocket --save
```
or
```shell
yarn add @push.rocks/smartsocket
```
2020-09-29 19:43:10 +00:00
## Usage
2024-04-14 16:20:36 +00:00
@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
2020-09-29 19:43:10 +00:00
2024-04-14 16:20:36 +00:00
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.
2020-09-29 19:43:10 +00:00
2024-04-14 16:20:36 +00:00
### Setting Up a WebSocket Server
To create a WebSocket server that clients can connect to:
2020-09-29 19:43:10 +00:00
```typescript
2024-04-14 16:20:36 +00:00
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}!` };
}
});
2020-09-29 19:43:10 +00:00
2024-04-14 16:20:36 +00:00
// Start the Smartsocket server
server.start().then(() => {
console.log('WebSocket server is running...');
2020-09-29 19:43:10 +00:00
});
2024-04-14 16:20:36 +00:00
```
### Creating a WebSocket Client
Create a client that connects to the WebSocket server and interacts with it:
2020-09-29 19:43:10 +00:00
2024-04-14 16:20:36 +00:00
```typescript
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'
2020-09-29 19:43:10 +00:00
});
2024-04-14 16:20:36 +00:00
client.connect().then(() => {
console.log('Connected to WebSocket server');
2020-09-29 19:43:10 +00:00
});
2024-04-14 16:20:36 +00:00
// 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');
2020-09-29 19:43:10 +00:00
```
2024-04-14 16:20:36 +00:00
### Handling Disconnections and Reconnections
@push.rocks/smartsocket provides mechanisms to handle client disconnections and attempt reconnections:
2020-09-29 19:43:10 +00:00
```typescript
2024-04-14 16:20:36 +00:00
client.on('disconnect', () => {
console.log('Disconnected from server. Attempting to reconnect...');
client.connect();
2020-09-29 19:43:10 +00:00
});
2024-04-14 16:20:36 +00:00
```
2020-09-29 19:43:10 +00:00
2024-04-14 16:20:36 +00:00
### Sending Binary Data
2020-09-29 19:43:10 +00:00
2024-04-14 16:20:36 +00:00
The library supports the transmission of binary data efficiently:
2020-09-29 19:43:10 +00:00
2024-04-14 16:20:36 +00:00
```typescript
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');
2020-09-29 19:43:10 +00:00
```
2024-04-14 16:20:36 +00:00
### 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](https://gitlab.com/pushrocks/smartsocket).
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.
## License and Legal Information
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](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.
2020-09-29 19:43:10 +00:00
2024-04-14 16:20:36 +00:00
### Company Information
2020-09-29 19:43:10 +00:00
2024-04-14 16:20:36 +00:00
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
2020-09-29 19:43:10 +00:00
2024-04-14 16:20:36 +00:00
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
2020-09-29 19:43:10 +00:00
2024-04-14 16:20:36 +00:00
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.