Skip to content

docs: add code sample for event usage#5026

Open
iamhabbeboy wants to merge 5 commits intowailsapp:masterfrom
iamhabbeboy:docs/add-event-code-sample
Open

docs: add code sample for event usage#5026
iamhabbeboy wants to merge 5 commits intowailsapp:masterfrom
iamhabbeboy:docs/add-event-code-sample

Conversation

@iamhabbeboy
Copy link

@iamhabbeboy iamhabbeboy commented Feb 28, 2026

Description

Summary of the Change

Added a sample code example demonstrating how to use runtime events for triggering and listening to events in the frontend.

Issue Fixed

There was no clear example explaining how to use runtime events between the Go backend and the frontend. This change improves documentation clarity by providing a practical starting point for new users.

Motivation and Context

As a new user to Wails, it took time figuring out the right path to the runtime package on the frontend.

To reduce confusion for beginners and provide a practical reference, a simple event usage example was added to demonstrate:

  • How to emit events from the Go backend

  • How to listen to events in the frontend

  • How both sides communicate using runtime events

Summary by CodeRabbit

  • Documentation
    • Added a Code Sample section to the EventsEmit reference with practical frontend examples for listening to and stopping an "auth:success" event and a UI trigger to emit it.
    • Included a Go example demonstrating how to emit an "auth:success" event with a sample payload.
    • Updated the changelog to record these documentation additions.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 28, 2026

📝 Walkthrough

Walkthrough

Adds documentation code samples to the EventsEmit reference: a TypeScript frontend example showing event subscription/unsubscription and UI trigger, and a Go example showing runtime.EventsEmit usage. Also adds a changelog entry noting the new example.

Changes

Cohort / File(s) Summary
Events documentation
website/docs/reference/runtime/events.mdx
Added a "Code Sample" section with a TypeScript frontend example (EventsOn/EventsOff listening to auth:success) and a Go example demonstrating runtime.EventsEmit with a sample payload.
Changelog
website/src/pages/changelog.mdx
Added an "Added" bullet under Unreleased documenting the new events usage example and referencing the PR.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Possibly related PRs

Suggested reviewers

  • leaanthony

Poem

🐇 I hopped through docs, pen bright and spry,
Added samples beneath the events sky,
TypeScript listens, Go sends the cheer,
Together they bridge frontend and back here! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive The pull request description includes a clear summary, motivation, and context, but is missing required checklist items and type-of-change selection from the template. Complete the checklist items (especially changelog and documentation update confirmations) and select the appropriate type of change to fully comply with the template requirements.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title clearly and concisely summarizes the main change: adding code samples for event usage documentation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
website/versioned_docs/version-v2.11.0/reference/runtime/events.mdx (1)

58-68: Prefer EventsOn's returned unsubscribe in React cleanup.

The current code at line 64 registers with EventsOn and cleans up via EventsOff("auth:success"), which removes all listeners for that event. Use the unsubscribe function returned by EventsOn instead for component-local cleanup that removes only this specific listener.

♻️ Safer cleanup pattern
   useEffect(() => {
       const handler = (event: any) => {
         // Handle event
         console.log(event);
       };
-  
-      EventsOn("auth:success", handler);
+      const cancel = EventsOn("auth:success", handler);
 
       return () => {
-        EventsOff("auth:success");
+        cancel();
       };
   },[])
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@website/versioned_docs/version-v2.11.0/reference/runtime/events.mdx` around
lines 58 - 68, The effect registers a listener with EventsOn but calls
EventsOff("auth:success") in cleanup which removes all listeners; instead
capture the unsubscribe function returned by EventsOn (e.g., const unsubscribe =
EventsOn("auth:success", handler)) and call unsubscribe() inside the cleanup to
only remove this component's handler; update the useEffect to use EventsOn's
returned unsubscribe rather than calling EventsOff("auth:success").
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@website/versioned_docs/version-v2.11.0/reference/runtime/events.mdx`:
- Around line 55-67: The sample imports the runtime incorrectly and misses the
EventsOff symbol; update the import to pull EventsOn and EventsOff from
"wailsjs/runtime/runtime" (replace the current import of EventsOn from
"../wailsjs/runtime") and ensure both EventsOn and EventsOff are imported
together (so the useEffect can call EventsOn("auth:success", handler) and return
EventsOff("auth:success")).

---

Nitpick comments:
In `@website/versioned_docs/version-v2.11.0/reference/runtime/events.mdx`:
- Around line 58-68: The effect registers a listener with EventsOn but calls
EventsOff("auth:success") in cleanup which removes all listeners; instead
capture the unsubscribe function returned by EventsOn (e.g., const unsubscribe =
EventsOn("auth:success", handler)) and call unsubscribe() inside the cleanup to
only remove this component's handler; update the useEffect to use EventsOn's
returned unsubscribe rather than calling EventsOff("auth:success").

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 033650d and de43ebf.

📒 Files selected for processing (1)
  • website/versioned_docs/version-v2.11.0/reference/runtime/events.mdx

@leaanthony
Copy link
Member

Thanks for this. You've added it to a locked version which may get regenerated. Could you please add it to the main "latest" docs so it's preserved. Please could you also add an entry to the changelog located at website/src/pages/changelog.mdx?

@iamhabbeboy iamhabbeboy force-pushed the docs/add-event-code-sample branch from 9032c5f to cf988a3 Compare February 28, 2026 22:54
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
website/docs/reference/runtime/events.mdx (2)

83-91: Minor formatting issues in Go code sample.

  • Line 83: "GO" → "Go" for proper language naming.
  • Line 85: Add space after // per Go conventions → // app.go
  • Indentation within the function body appears inconsistent.
Suggested fix
-GO
+Go
 ```go
-  //app.go
-  import "github.com/wailsapp/wails/v2/pkg/runtime"
+// app.go
+import "github.com/wailsapp/wails/v2/pkg/runtime"

-   func (a *App) Auth() {
-	   runtime.EventsEmit(a.ctx, "auth:success", "user-123")
-   }
+func (a *App) Auth() {
+    runtime.EventsEmit(a.ctx, "auth:success", "user-123")
+}

</details>

<details>
<summary>🤖 Prompt for AI Agents</summary>

Verify each finding against the current code and only fix it if needed.

In @website/docs/reference/runtime/events.mdx around lines 83 - 91, The Go code
sample has formatting issues: change the language label "GO" to "Go", update the
comment to "// app.go", fix import indentation, and normalize the function
formatting for func (a *App) Auth() { ... } so the body uses consistent 4-space
or tab indentation and the closing brace aligns with the func declaration;
specifically update the snippet around the Auth method and the
runtime.EventsEmit call so it matches Go style and the rest of the docs.


</details>

---

`55-81`: **Consider using the cancel function returned by `EventsOn`.**

Per the documentation above (line 14), `EventsOn` returns a cancel function. Using this is more idiomatic and safer than calling `EventsOff` by event name, which could inadvertently unregister other listeners on the same event.

Also, a few minor formatting nits:
- Line 76: Add space before `[]` → `}, [])`
- The button JSX on line 79 appears orphaned; consider wrapping it in a comment or showing a minimal component structure for clarity.



<details>
<summary>Suggested improvement</summary>

```diff
  useEffect(() => {
      const handler = (event: any) => {
        // Handle event
        console.log(event);
      };
  
-      EventsOn("auth:success", handler);
+      const cancelListener = EventsOn("auth:success", handler);

      return () => {
-        EventsOff("auth:success");
+        cancelListener();
      };
-  },[])
+  }, []);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@website/docs/reference/runtime/events.mdx` around lines 55 - 81, Update the
example to use the cancel function returned by EventsOn instead of calling
EventsOff by name: call const cancel = EventsOn("auth:success", handler) and
invoke cancel() in the useEffect cleanup; update the example to reference
EventsOn, EventsOff, Auth and useEffect so readers know where the cancel comes
from, remove the EventsOff call, and ensure the useEffect cleanup is formatted
with a space "}, [])"; also move or wrap the button JSX (the Auth button) inside
a minimal component structure or comment so it isn't rendered as an orphan
outside the component.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@website/docs/reference/runtime/events.mdx`:
- Around line 83-91: The Go code sample has formatting issues: change the
language label "GO" to "Go", update the comment to "// app.go", fix import
indentation, and normalize the function formatting for func (a *App) Auth() {
... } so the body uses consistent 4-space or tab indentation and the closing
brace aligns with the func declaration; specifically update the snippet around
the Auth method and the runtime.EventsEmit call so it matches Go style and the
rest of the docs.
- Around line 55-81: Update the example to use the cancel function returned by
EventsOn instead of calling EventsOff by name: call const cancel =
EventsOn("auth:success", handler) and invoke cancel() in the useEffect cleanup;
update the example to reference EventsOn, EventsOff, Auth and useEffect so
readers know where the cancel comes from, remove the EventsOff call, and ensure
the useEffect cleanup is formatted with a space "}, [])"; also move or wrap the
button JSX (the Auth button) inside a minimal component structure or comment so
it isn't rendered as an orphan outside the component.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5df113d and cf988a3.

📒 Files selected for processing (2)
  • website/docs/reference/runtime/events.mdx
  • website/src/pages/changelog.mdx
🚧 Files skipped from review as they are similar to previous changes (1)
  • website/src/pages/changelog.mdx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants