Skip to content
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

[0.76] WebSocket (MQTT) connection does not emit ‘offline’ event when the app goes to the background #49243

Open
19931029 opened this issue Feb 7, 2025 · 3 comments
Labels
Needs: Author Feedback Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Newer Patch Available

Comments

@19931029
Copy link

19931029 commented Feb 7, 2025

Description

In React Native 0.73, WebSocket-based MQTT connections worked correctly in the background.
When the app moved to the background, the connection eventually dropped, and the offline event was emitted.

However, after updating to React Native 0.76, the offline event is not emitted, and the MQTT connection is not automatically closed when the app moves to the background.

Possible Causes:
• The behavior of setImmediate has changed, affecting WebSocket execution in the background.
• The introduction of queueReactNativeMicrotask (starting from RN 0.74) may have affected this behavior.

Steps to reproduce

1.	Set up react-native-mqtt (or WebSocket-based MQTT) in a React Native 0.76 environment.
2.	Start the app and establish a WebSocket connection.
3.	Use AppState.addEventListener("change", callback) to track app state changes.
4.	Move the app to the background.
5.	The offline event does not fire.

React Native Version

0.76.0

Affected Platforms

Runtime - Android

Output of npx react-native info

System:
  OS: macOS 14.4.1
  CPU: (8) arm64 Apple M1 Pro
  Memory: 104.77 MB / 16.00 GB
  Shell:
    version: 3.2.57
    path: /bin/bash
Binaries:
  Node:
    version: 20.13.1
    path: /usr/local/bin/node
  Yarn:
    version: 3.6.4
    path: /usr/local/bin/yarn
  npm:
    version: 10.5.2
    path: /usr/local/bin/npm
  Watchman:
    version: 2022.07.04.00
    path: /usr/local/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.3
    path: /Users/snowwhite/.rvm/gems/ruby-2.7.4/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.5
      - iOS 17.5
      - macOS 14.5
      - tvOS 17.5
      - visionOS 1.2
      - watchOS 10.5
  Android SDK:
    API Levels:
      - "28"
      - "30"
      - "31"
      - "33"
      - "34"
      - "35"
    Build Tools:
      - 29.0.2
      - 30.0.2
      - 30.0.3
      - 31.0.0
      - 33.0.0
      - 33.0.1
      - 33.0.3
      - 34.0.0
      - 35.0.0
    System Images:
      - android-30 | Intel x86 Atom_64
      - android-30 | Google APIs Intel x86 Atom
      - android-31 | Google APIs Intel x86 Atom_64
    Android NDK: 22.1.7171670
IDEs:
  Android Studio: 2024.2 AI-242.23339.11.2421.12550806
  Xcode:
    version: 15.4/15F31d
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.11
    path: /usr/bin/javac
  Ruby:
    version: 2.7.4
    path: /Users/snowwhite/.rvm/rubies/ruby-2.7.4/bin/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 15.1.3
    wanted: ^15.0.0
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.76.6
    wanted: ^0.76.0
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: false

Stacktrace or Logs

none

Reproducer

none

Screenshots and Videos

import MQTT from 'react-native-mqtt';

const options = {
  host: 'broker.hivemq.com',
  port: 1883,
  clientId: 'test-client',
  keepalive: 60,
};

const client = MQTT.createClient(options);

client.on('connect', () => {
  console.log('MQTT Connected');
});

client.on('offline', () => {
  console.log('MQTT Disconnected'); // ✅ This works in RN 0.73, but NOT in RN 0.76
});

client.connect();
• This worked correctly in React Native 0.73.
• After updating to 0.76, Hermes might be handling setImmediate differently.
• The introduction of queueReactNativeMicrotask in RN 0.74 might have changed how WebSocket runs in the background.
@react-native-bot react-native-bot added Needs: Author Feedback Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Newer Patch Available labels Feb 7, 2025
@react-native-bot
Copy link
Collaborator

Tip

Newer version available: You are on a supported minor version, but it looks like there's a newer patch available - 0.76.7. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@react-native-bot
Copy link
Collaborator

Tip

Newer version available: You are on a supported minor version, but it looks like there's a newer patch available - undefined. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@react-native-bot
Copy link
Collaborator

Warning

Missing reproducer: We could not detect a reproducible example in your issue report. Please provide either:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Author Feedback Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Newer Patch Available
Projects
None yet
Development

No branches or pull requests

2 participants