Skip to main content

React to live events

Build a widget that thanks your most recent follower and flashes when a new one comes in, then test it with the simulator instead of a real follow.

Start with Build your first widget if you haven't.

1. Declare the binding​

Open the Bindings surface and toggle on Follow. A widget only receives events it declares. If you use an event in code without declaring it, the console warns you and it won't arrive live.

2. Show the latest follower​

useLatestEvent gives you the most recent payload and re-renders when a new one arrives.

// main.tsx
import { useLatestEvent, Event } from '@widget';

export default function Main() {
const follow = useLatestEvent(Event.Follow);
const name = follow?.user.displayName;

return (
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
width: '100%',
height: '100%',
fontSize: '40px',
fontWeight: 800,
color: '#fff',
}}
>
{name ? `Thanks for the follow, ${name}!` : 'Waiting for a follower…'}
</div>
);
}

3. Test it with the simulator​

On the Simulate surface:

  1. Select the Follow event.
  2. Edit the payload's displayName.
  3. Click Fire.

The preview thanks that name. Burst fires several in a row.

tip

The simulator shows each event's payload shape: a follow has a user, a cheer has bits, a raid has viewers. The full catalogue is in Event bindings.

4. Add a flash on each new follow​

To react to the moment a follow happens, use useEvent in the template, or registerBinding in the core.

// main.tsx
import { useState } from 'react';
import { useEvent, Event } from '@widget';

export default function Main() {
const [flashing, setFlashing] = useState(false);
const [name, setName] = useState<string | null>(null);

useEvent(Event.Follow, (payload) => {
setName(payload.user.displayName);
setFlashing(true);
setTimeout(() => setFlashing(false), 800);
});

return (
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
width: '100%',
height: '100%',
fontSize: '40px',
fontWeight: 800,
color: '#fff',
transition: 'filter 200ms ease, transform 200ms ease',
filter: flashing ? 'brightness(1.8)' : 'none',
transform: flashing ? 'scale(1.08)' : 'scale(1)',
}}
>
{name ? `Thanks for the follow, ${name}!` : 'Waiting for a follower…'}
</div>
);
}

Fire a Follow from the simulator to see it pulse.

5. Reading live values​

Live values such as the viewer count, stream title and goals are read the same way:

import { useLatestEvent, Event } from '@widget';

export default function Main() {
const stats = useLatestEvent(Event.Stats);
return <div>{stats?.viewers ?? 0} watching now</div>;
}

Declare Channel Stats on the Bindings surface first.

6. Build and place it​

Click Build and add the widget to an overlay as before. Real follows drive it when you're live.

Next​