create
Deprecated: Use new Observable() instead
new Observable() instead⚠️ Important:
Observable.create()was deprecated in RxJS v6.4.0. Use thenew Observable()constructor instead.
signature: new Observable(subscribe: (observer: Observer) => TeardownLogic)
new Observable(subscribe: (observer: Observer) => TeardownLogic)Create a custom observable by defining subscription behavior.
💡 Tips
Use for bridging non-reactive APIs: Custom observables shine when wrapping callbacks, event emitters, WebSockets, or other non-observable data sources
Always return teardown logic: Return a cleanup function to prevent memory leaks when subscriptions end
Why use a custom observable?
Custom observables are your bridge between the non-reactive world and RxJS. Think of them as adapters—when you have a data source that doesn't speak "Observable" (like a WebSocket connection, a third-party library with callbacks, or a browser API like Geolocation), creating a custom observable lets you wrap it up and make it play nicely with the rest of your reactive code.
Here's the thing: you'll rarely need to create custom observables. RxJS already provides creation operators for most common scenarios. Timers, events, promises, arrays, and more. But when you encounter an API that doesn't fit any existing operator, custom observables give you fine-grained control. You decide exactly when to emit values (observer.next()), how to handle errors (observer.error()), and what cleanup should happen when someone unsubscribes.
One way to think about this is like writing a translator. The non-reactive API speaks one language, your Observable streams speak another, and your custom observable sits in the middle making sure they understand each other. When implementing the cleanup function, remember that this is your chance to be a good citizen. Close connections, cancel timers, remove listeners. It's like turning off the lights when you leave a room.
In essence, custom observables are powerful but should be used sparingly. If an existing creation operator can do the job, use that. But when you need that extra control—when you're integrating with legacy code, third-party libraries, or unusual data sources, this is your tool.
Examples
Example 1: Simple custom observable that emits multiple values
Example 2: Observable with proper cleanup
Example 3: Wrapping a callback-based API
Example 4: Creating an observable from a WebSocket
Related Recipes
Additional Resources
Observable 📰 - Official docs
Observable Constructor 📰 - Official constructor docs
📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/packages/observable/src/observable.ts
Last updated