Websockets
Websocket support in Rhino is provided by ActionCable. By default websockets are authenticated by not authorized (you must ensure the user has access to the data they are requesting).
Installation
Websockets are supported out of the box as long as Redis is available in your environment. You will need to set the allowed origins in your environment configuration.
config/environments/production.rb
config.action_cable.allowed_request_origins = ['https://example.com']
Creating a channel
rails g channel Test
Connecting to a channel
The front end provides a useCable
hook that can be used to connect to a channel.
const consumer = useCable();
const [messages, setMessages] = useState([]);
useEffect(() => {
if (!resource?.id) return;
consumer.subscriptions.create(
{ channel: "TestChannel", id: resource.id },
{
connected() {},
received(data) {
setMessages((currentValues) => {
const a = [...currentValues, data];
return a;
});
},
}
);
}, [consumer, resource.id]);