
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.
react-use-action-cable-ts
Advanced tools
Hooks to easily use Rails Action Cable in your React application
Hooks to use Rails Action Cable in your React application.
npm i react-use-action-cable-ts
To connect to an Action Cable, simply call the useActionCable hook with the URL you wish to connect to. If you want to be able to use this Action Cable throughout your application consider implementing it in a context.
import React, { useEffect } from 'react';
import { useActionCable, useChannel } from 'react-use-action-cable-ts';
export default function index() {
const { actionCable } = useActionCable('ws://localhost:3000/cable');
}
Provide the useChannel hook with the previously created actionCable
. You then get access to the (un)subscribe and send functions. In the example below we immediately subscribe to the channel 'ChannelName' on the first render.
...
const { subscribe, unsubscribe, send } = useChannel(actionCable)
useEffect(() => {
subscribe({
channel: 'ChannelName',
param2: '...',
param3: '...'
}, {
received: (data) => console.log(data),
// Custom callbacks can be added for 'initialized', 'connected', and 'disconnected'
})
return () => {
unsubscribe()
}
}, [])
...
To send data to the channel that we are subscribe to we can use the send
function as below.
send({
action: 'ping',
payload: {}, // Optional
useQueue: true // Optional, default: false
})
When setting useQueue to true
the message will be added to a queue and will be sent as soon as possible. That is, immediately when the websocket is connected. If the websocket is not connected at the time send()
is called it will be sent as soon as the websocket reconnects.
FAQs
Hooks to easily use Rails Action Cable in your React application
The npm package react-use-action-cable-ts receives a total of 272 weekly downloads. As such, react-use-action-cable-ts popularity was classified as not popular.
We found that react-use-action-cable-ts 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.