CableStore gives you Svelte stores that connect to your Rails application via ActionCable and can be manipulated via your server-side application code.
Note It's still early, and there's currently no way to configure the Websocket URL (it uses the Rails default
/cable
).
Add this line to your application's Gemfile:
gem 'cablestore'
And then execute:
$ bundle install
Install the package:
$ yarn add @buhrmi/cablestore
To subscribe to ActiveRecord object, you have to pass the signed Global ID to the frontend. For example, via a controller action:
def show
@sgid = User.to_signed_global_id(expires_in: nil).to_s
end
Then you can subscribe to it in your client-side code:
import { subscribe } from '@buhrmi/cablestore'
const store = subscribe(sgid, nil, 'current_user')