Store Extension and Viewer State
Muxy lets you define your own state data and maintains it in real time.
The Muxy server tracks the state of your extension in real time. You can use MEDKit JavaScript SDK methods to define, store, and retrieve state values. States are stored as JSON blobs, containing developer-defined key-value pairs. There are no predefined keys. You are responsible for defining what data you want to track, and performing any validation of values.
State Stores
Four state stores vary in scope and accessibility. Read and write access to state values depends on the role of the current user when one of these methods is called.
- Extension: Keeps state as shared across all instances of an extension.
- Channel: Keeps state on a per-channel basis.
- Viewer: Keeps state for viewers on a specific channel.
- ExtensionViewer: Keeps per-viewer state that is accessible across every instance of the extension on Twitch.
A user can access each of these stores individually, depending on their role. In addition, any user can retrieve the current content from all four stores at once.
Access to State Stores
Use MEDKit methods to retrieve an aggregate of all state information, or to set and get state from each store. Some set methods are only available to users with specific roles.
Store | Scope and access |
---|---|
AllState | Aggregates and returns all of the state stores. Read-only, get available to all users. |
ExtensionState | Keeps state as shared across all instances of an extension. Get available to all users. Set available to broadcaster whose user ID matches the extension owner or admin . |
ChannelState | Keeps state on a per-channel basis. Get available to all users. Set available only to broadcaster . |
ViewerState | Keeps state on a per-viewer basis. Get available to all users. Set available to viewers of a specific channel. |
ExtensionViewerState | Keeps per-viewer state across every instance of the extension on Twitch. Get and set available to viewers across all channels. |
State Access Examples
The following examples show how to access the state stores with Muxy.SDK
methods, and the format of the response. Developers are responsible for defining keys and expected values in the JSON content.
These examples assume a variable muxy
set to a Muxy.SDK
instance.
All State
Get |
---|
js medkit.getAllState().then(state => { console.log(state); }); Response json { extension: { enable_feature: false }, channel: { channel_specific_setting: "abc" }, viewer: { viewer_specific_setting: 1234 } } |
Extension State
Keeps state as shared across all instances of an extension.
Get available to any role. Set available to broadcaster
whose user ID matches the extension owner
or admin
.
Get | Set |
---|---|
js medkit.getExtensionState().then(state => { console.log(state); }); Response json { "sample_field": "test" } | js const newState = { sample_field: 'test' }; sdk.setExtensionState(newState).then(state => { console.log(state); }); Response json {} |
Channel State
Keeps state on a per-channel basis. Can be set only by a user with broadcaster
role. Get available to any role.
Get | Set |
---|---|
javascript medkit.getChannelState().then(state => { console.log(state); }); Response json { "sample_field": "test" } | js const newState = { sample_field: 'test' }; medkit.setChannelState(newState).then(state => { console.log(state); }); Response json {} |
Extension Viewer State
Keeps per-viewer state across every instance of the extension on Twitch. Available to any user role for set or get.
Get | Set |
---|---|
js medkit.getExtensionViewerState().then(state => { console.log(state); }); Response json { "sample_field": "test" } | js const newState = { sample_field: 'test' }; medkit.setExtensionViewerState(newState).then(state => { console.log(state); }); Response json {} |
Viewer State
Keeps per-viewer state for a specific channel. Available to any user role for set or get.
Get | Set |
---|---|
js medkit.getViewerState().then(state => { console.log(state); }); Response json { "sample_field": "test" } | js medkit.getViewerState().then(state => { console.log(state); }); Response json { "sample_field": "test" } |
Updated about 3 years ago