Prompt
When using MessageBus for network communication, follow these essential practices to ensure reliable message handling and prevent memory leaks:
- Use consistent channel names between subscribe and unsubscribe calls to avoid subscription leaks
- Include the third argument when subscribing for better message bus practices
- Always pair subscriptions with unsubscriptions in component lifecycle methods
Example of proper MessageBus usage:
constructor() {
super(...arguments);
const settingName = this.setting.setting;
if (this.canSubscribeToSettingsJobs) {
// Use third argument for better practice
this.messageBus.subscribe(`/site_setting/${settingName}/process`, this.onMessage, this);
}
}
willDestroy() {
super.willDestroy(...arguments);
const settingName = this.setting.setting;
if (this.canSubscribeToSettingsJobs) {
// Ensure channel name matches subscription exactly
this.messageBus.unsubscribe(`/site_setting/${settingName}/process`, this.onMessage);
}
}
This prevents subscription leaks, improves message handling reliability, and follows MessageBus best practices for network communication management.