-
Notifications
You must be signed in to change notification settings - Fork 83
Add Observable subscriptions with RxJS support #1357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mahmoud-ghalayini
merged 4 commits into
RobotWebTools:develop
from
mahmoud-ghalayini:add-observable-subscriptions-with-rxjs-support-1347
Dec 18, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
example/topics/subscriber/subscription-observable-example.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (c) 2025 Mahmoud Alghalayini. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const rclnodejs = require('../../../index.js'); | ||
| const { throttleTime, map, filter, bufferCount } = require('rxjs'); | ||
|
|
||
| async function main() { | ||
| await rclnodejs.init(); | ||
|
|
||
| const node = rclnodejs.createNode('observable_subscription_example_node'); | ||
|
|
||
| // Basic observable subscription | ||
| const obsSub = node.createObservableSubscription( | ||
| 'std_msgs/msg/String', | ||
| 'topic' | ||
| ); | ||
|
|
||
| // Example 1: Throttled messages (max 2/sec) | ||
| obsSub.observable | ||
| .pipe( | ||
| throttleTime(500), | ||
| map((msg) => msg.data) | ||
| ) | ||
| .subscribe((data) => { | ||
| console.log('[Throttled]', data); | ||
| }); | ||
|
|
||
| // Example 2: Filtered messages (only containing "ROS") | ||
| // Note: RxJS filter() operates at the application level after messages are received. | ||
| // For more efficient filtering at the DDS middleware level (reducing network traffic), | ||
| // use the contentFilter option. See: tutorials/content-filtering-subscription.md | ||
| obsSub.observable | ||
| .pipe( | ||
| map((msg) => msg.data), | ||
| filter((data) => data.includes('ROS')) | ||
| ) | ||
| .subscribe((data) => { | ||
| console.log('[Filtered]', data); | ||
| }); | ||
|
|
||
| // Example 3: Batched messages (every 3 messages) | ||
| obsSub.observable | ||
| .pipe( | ||
| map((msg) => msg.data), | ||
| bufferCount(3) | ||
| ) | ||
| .subscribe((batch) => { | ||
| console.log('[Batched]', batch.length, 'messages'); | ||
| }); | ||
|
|
||
| console.log('Observable subscription created on /topic'); | ||
| console.log( | ||
| 'Run: ros2 topic pub /topic std_msgs/msg/String "{data: Hello ROS}" -r 5' | ||
| ); | ||
|
|
||
| rclnodejs.spin(node); | ||
| } | ||
|
|
||
| main(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Copyright (c) 2025 Mahmoud Alghalayini. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const { Subject } = require('rxjs'); | ||
|
|
||
| /** | ||
| * A wrapper that provides RxJS Observable support for ROS 2 subscriptions. | ||
| * This class wraps a standard Subscription and emits messages through an Observable. | ||
| * | ||
| * @class ObservableSubscription | ||
| * @hideconstructor | ||
| */ | ||
| class ObservableSubscription { | ||
minggangw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #subscription; | ||
| #subject; | ||
| #destroyed; | ||
|
|
||
| /** | ||
| * Create an ObservableSubscription wrapper. | ||
| * @param {Subscription} subscription - The underlying ROS 2 subscription | ||
| */ | ||
| constructor(subscription) { | ||
| this.#subscription = subscription; | ||
| this.#subject = new Subject(); | ||
| this.#destroyed = false; | ||
| } | ||
|
|
||
| /** | ||
| * Get the RxJS Observable for this subscription. | ||
| * Use this to pipe operators and subscribe to messages. | ||
| * @type {Observable} | ||
| */ | ||
| get observable() { | ||
| return this.#subject.asObservable(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the underlying ROS 2 subscription. | ||
| * @type {Subscription} | ||
| */ | ||
| get subscription() { | ||
| return this.#subscription; | ||
| } | ||
|
|
||
| /** | ||
| * Get the topic name. | ||
| * @type {string} | ||
| */ | ||
| get topic() { | ||
| return this.#subscription.topic; | ||
| } | ||
|
|
||
| /** | ||
| * Check if this observable subscription has been destroyed. | ||
| * @type {boolean} | ||
| */ | ||
| get isDestroyed() { | ||
| return this.#destroyed; | ||
| } | ||
|
|
||
| /** | ||
| * Internal method to emit a message to subscribers. | ||
| * Called by the subscription's processResponse. | ||
| * @private | ||
| * @param {any} message - The message to emit | ||
| */ | ||
| _emit(message) { | ||
| if (!this.#destroyed) { | ||
| this.#subject.next(message); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Complete the observable and clean up resources. | ||
| * After calling this, no more messages will be emitted. | ||
| */ | ||
| complete() { | ||
| if (!this.#destroyed) { | ||
| this.#destroyed = true; | ||
| this.#subject.complete(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Alias for complete() for consistency with RxJS naming. | ||
| */ | ||
| destroy() { | ||
| this.complete(); | ||
| } | ||
| } | ||
|
|
||
| module.exports = ObservableSubscription; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the ROS2 DDS also supports subscription content filtering, which is more efficient I believe, shall we point out the difference between the two
filter?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the readme to have an example for content filter + RxJS