integration-websocket-rest-api
v1.0.51
Published
A JavaScript library for easy integration of REST API and WebSocket communication with state management in JS applications.
Downloads
9
Maintainers
Readme
REST API, WebSocket and Redux Integration Package
This package provides a seamless integration of REST API and WebSocket functionality into your applications. It leverages the power of Redux for state management, allowing you to easily manage actions for both REST and WebSocket interactions. The package can be published to npm and installed in any compatible application, such as Angular applications. Additionally, this package includes features like reconnection logic and data formatting.
Installation
Install this package with npm
npm install integration-websocket-rest-api
Usage/Examples
API Client
The package includes an API client (ApiClient) for making REST API requests. It uses Axios for HTTP requests and provides features like request retries, timeouts, and customizable error handling. The API client is designed to work seamlessly with Redux for state management.
import { Component, OnInit } from "@angular/core";
import { ApiClient } from "integration-websocket-rest-api";
@Component({
selector: "app-my-component",
templateUrl: "./my-component.component.html",
styleUrls: ["./my-component.component.css"],
})
export class MyComponent implements OnInit {
constructor() {}
ngOnInit(): void {
const apiUrl = "https://example_url.com";
const apiClient = new ApiClient(apiUrl);
// Set global headers if needed
apiClient.setGlobalHeaders({
Authorization: "Bearer YOUR_ACCESS_TOKEN",
});
// Make a GET request
apiClient
.sendRequest("GET" / "POST", null / data, "other congigurations")
.pipe(map((messages) => console.log(messages)));
}
}
WebSocket Client
The package also includes a WebSocket client (WebSocketClient) for real-time communication. It uses the websocket library and integrates seamlessly with Redux for managing WebSocket messages.
import { Component, OnInit } from "@angular/core";
import { WebSocketClient } from "integration-websocket-rest-api";
@Component({
selector: "app-my-component",
templateUrl: "./my-component.component.html",
styleUrls: ["./my-component.component.css"],
})
export class MyComponent implements OnInit {
constructor() {}
ngOnInit(): void {
const wsUrl = "wss://socket.example.com";
const wsClient = new WebSocketClient(wsUrl);
// Open WebSocket connection
wsClient.openConnection();
// Send messages
wsClient.sendMessage(action.message);
// Listen for incoming WebSocket messages
this.wsClient.getMessage((message: any) => {
console.log(`Received message: ${message}`);
});
}
}
State Management
The package utilizes Redux for state management. It includes actions (wsMessageReceived, apiRequestSuccess, apiRequestFailure) and reducers for handling WebSocket messages and API responses inside the package.
Configuration
Both the API client and WebSocket client support configuration options. You can customize headers, retries, timeouts, and more. Refer to the respective client files for available configuration methods.
Support
For support, email [email protected] or [email protected] or [email protected] or [email protected].