Create Your Own Source
A source captures events from an environment (browser, server, third-party API) and forwards them to the walkerOS collector.
The Source Interface
Sources are async functions that receive configuration and environment, then return a source instance:
The returned instance must implement:
Types Bundle
Sources use a Types interface to bundle all TypeScript types:
The 4 type parameters:
- Settings: Source configuration options
- Mapping: Event mapping (usually
neverfor sources) - Push: External push signature (what
source.pushexposes) - Env: Internal dependencies (what source calls via
env.elb)
Environment Parameter
The env parameter is always provided by the collector and contains:
elb: Required - function to send events to collector (provided automatically)- Custom dependencies: Optional - any APIs your source needs (you provide via config)
The collector always provides env.elb. You provide other dependencies (like window, document, custom APIs) when configuring the source.
Minimal Example
Complete Example: Event API Source
Capturing events from a third-party API with event listeners:
Using Your Source
Testing Your Source
Test Utilities
Test Example
Common Patterns
Polling for API Readiness
When the external API isn't immediately available:
Source as Adapter Pattern
Sources bridge external systems and the collector:
External System ←→ Source (Adapter) ←→ Collector
Two interfaces:
-
External (
source.push): Platform-specific signature- Browser:
push(elem, data, options)→ ReturnsPromise<PushResult> - Server:
push(req, res)→ ReturnsPromise<void>(writes HTTP response) - Your choice: Match your environment's needs
- Browser:
-
Internal (
env.elb): Standard collector interface- Always
Elb.Fn- same across all sources - Sources translate external inputs → standard events →
env.elb(event)
- Always
Example signatures:
The Push type parameter defines what your source exposes externally. Internally, all sources use env.elb to forward to the collector.
Key Concepts
- env is always provided: Collector ensures env exists with elb
- Validate custom dependencies: Only check optional env properties your source needs
- Types bundle: Use 4-parameter pattern for full type safety
- Adapter pattern: External push adapts to environment, internal env.elb stays standard
- Cleanup: Implement
destroy()to remove listeners - Stateless: Let collector manage state
Next Steps
- Review Browser Source for DOM patterns
- Review DataLayer Source for interception patterns
- Learn about creating destinations