Hooks to use Rails Action Cable in your React (Native) application
Installations
npm install @aersoftware/react-use-action-cable
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
16.13.2
NPM Version
8.9.0
Score
72
Supply Chain
82.5
Quality
77.5
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
HTML (52.91%)
JavaScript (36.88%)
CSS (10.21%)
Developer
aersoftware
Download Statistics
Total Downloads
41,487
Last Day
72
Last Week
314
Last Month
1,273
Last Year
23,211
GitHub Statistics
24 Stars
17 Commits
8 Forks
3 Watching
1 Branches
1 Contributors
Bundle Size
13.98 kB
Minified
4.41 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.4
Package Id
@aersoftware/react-use-action-cable@1.0.4
Unpacked Size
22.24 kB
Size
5.57 kB
File Count
4
NPM Version
8.9.0
Node Version
16.13.2
Publised On
31 May 2023
Total Downloads
Cumulative downloads
Total Downloads
41,487
Last day
-16.3%
72
Compared to previous day
Last week
-17.2%
314
Compared to previous week
Last month
-2.8%
1,273
Compared to previous month
Last year
62.4%
23,211
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Peer Dependencies
1
React Use Action Cable
Hooks to use Rails Action Cable in your React (Native) application.
Installation
1npm install @aersoftware/react-use-action-cable --save
Usage
Connecting to a websocket
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.
1import React, { useEffect } from 'react'; 2import { useActionCable, useChannel } from '@aersoftware/react-use-action-cable'; 3 4export default function index() { 5 const { actionCable } = useActionCable('ws://localhost:3000/cable'); 6}
Subscribe to a channel
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.
1... 2 const { subscribe, unsubscribe, send } = useChannel(actionCable) 3 4 useEffect(() => { 5 subscribe({ 6 channel: 'ChannelName', 7 param2: '...', 8 param3: '...' 9 }, { 10 received: (data) => console.log(data), 11 // Custom callbacks can be added for 'initialized', 'connected', and 'disconnected' 12 }) 13 return () => { 14 unsubscribe() 15 } 16 }, []) 17...
Sending data
To send data to the channel that we are subscribe to we can use the send
function as below.
1send({ 2 action: 'ping', 3 payload: {}, // Optional 4 useQueue: true // Optional, default: false 5})
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.
No vulnerabilities found.
No security vulnerabilities found.