
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@evandrolg/react-web-socket
Advanced tools
react-web-socket is a lightweight React provider that integrates WebSocket functionality into your React application, allowing you to easily manage WebSocket connections, send and receive messages, and track connection status using React hooks and context.
To install the package, run:
npm install @evandrolg/react-web-socket
Wrap your application (or a part of it) with the WebSocketProvider
to provide WebSocket context to your components.
import React from 'react';
import { WebSocketProvider } from 'react-web-socket';
const App = () => {
return (
<WebSocketProvider url="wss://your-websocket-url">
<YourComponent />
</WebSocketProvider>
);
};
export default App;
The useWebSocket
hook allows you to access WebSocket state and send messages from within your components.
import React, { useEffect } from 'react';
import { useWebSocket } from 'react-web-socket';
const YourComponent = () => {
const { lastMessage, sendMessage, status, error } = useWebSocket();
useEffect(() => {
if (lastMessage) {
console.log('Received:', lastMessage);
}
}, [lastMessage]);
const handleSendMessage = () => {
sendMessage({ type: 'HELLO', content: 'Hello, WebSocket!' });
};
return (
<div>
<h1>WebSocket Status: {status}</h1>
{error && <p>Error: {error.message}</p>}
<button onClick={handleSendMessage}>Send Message</button>
{lastMessage && <p>Last Message: {JSON.stringify(lastMessage)}</p>}
</div>
);
};
export default YourComponent;
The WebSocketProvider
component provides the WebSocket context to your application.
url
(string, required): The WebSocket server URL.children
(ReactNode, required): The child components that will have access to the WebSocket context.The useWebSocket
hook gives access to WebSocket functionality.
lastMessage
: The most recent WebSocket message received (parsed as JSON).sendMessage(message: object)
: Sends a JSON message through the WebSocket connection.status
: The current status of the WebSocket connection (CONNECTING
, OPEN
, or CLOSED
).error
: Any WebSocket error that may have occurred.The WebSocketStatus
enum can have the following values:
CONNECTING
: The WebSocket connection is in the process of being established.OPEN
: The WebSocket connection is open and ready to communicate.CLOSED
: The WebSocket connection is closed.Here's a full example of how to use react-web-socket in your project:
import React, { useEffect } from 'react';
import { WebSocketProvider, useWebSocket } from 'react-web-socket';
const WebSocketComponent = () => {
const { lastMessage, sendMessage, status, error } = useWebSocket();
useEffect(() => {
if (lastMessage) {
console.log('Received:', lastMessage);
}
}, [lastMessage]);
const handleSendMessage = () => {
sendMessage({ message: 'Hello from the client!' });
};
return (
<div>
<h1>WebSocket Status: {status}</h1>
{error && <p>Error: {error.message}</p>}
<button onClick={handleSendMessage}>Send Message</button>
{lastMessage && <p>Last Message: {JSON.stringify(lastMessage)}</p>}
</div>
);
};
const App = () => {
return (
<WebSocketProvider url="wss://your-websocket-url">
<WebSocketComponent />
</WebSocketProvider>
);
};
export default App;
FAQs
A lightweight WebSocket provider for React.
The npm package @evandrolg/react-web-socket receives a total of 0 weekly downloads. As such, @evandrolg/react-web-socket popularity was classified as not popular.
We found that @evandrolg/react-web-socket demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.