Yes, there are several mechanisms in this repository that allow the bridge to function even when the Google Messages app is in the background. Here's a breakdown of the key techniques:
1. Long Polling:
- The bridge uses a long-polling connection to receive real-time updates from the Google Messages service. This is handled in
pkg/libgm/longpoll.go.
- Instead of making frequent short requests, a long-polling request is kept open, and the server pushes updates through that connection as they occur. This allows the bridge to receive messages as soon as they arrive on the phone, even if the app isn't in the foreground.
- The
doLongPoll function in pkg/libgm/longpoll.go is responsible for initiating and maintaining this connection.
- When the connection closes, the
doLongPoll function will automatically attempt to reconnect.
2. Ditto Pinger (Keep-Alive):
- To keep the long polling connection alive and detect if the phone goes offline, the bridge uses a "ditto pinger" implemented in
pkg/libgm/longpoll.go.
- The pinger sends periodic "ditto" messages to the Google Messages server (
NotifyDittoActivity in pkg/libgm/client.go) which the phone responds to.
- This is crucial because the long-polling connection can be dropped due to network issues or the phone going to sleep.
- If a ping fails, the bridge will send a
PhoneNotResponding event. It will also use an exponential backoff strategy for sending new pings if the phone is not responding. When a ping succeeds after a failure, a PhoneRespondingAgain event is sent. This is intended to make the bridge very reliable.
- The ditto pinger also short-circuits and sends a ping immediately when an outgoing request to the phone times out.
- The pinger runs in its own goroutine in the
Loop function.
3. Data Receive Check:
- The
shouldDoDataReceiveCheck and bumpNextDataReceiveCheck functions, also in pkg/libgm/longpoll.go, are used to perform an additional check for data from the phone if no new data has been received recently.
- If the check determines it's necessary, it will send a
GET_UPDATES message to the server.
- This helps prevent the bridge from getting into a state where it stops receiving new messages from the phone.
- This check is performed either after
DefaultBugleDefaultCheckInterval (currently 2 hours and 55 minutes) has passed since the last check, or when the connection has been open for more than 5 minutes, or if the connection has been open for over a minute and it has been over 30 minutes since the last check.
- This mechanism helps to handle cases where the long polling connection may be working, but the phone has not sent any new messages to the bridge for a while, so the bridge forces the phone to send any updates.
4. Session Handling and Re-Authentication:
- The bridge stores session data and uses it to maintain its connection to Google Messages.
- The
SessionHandler in pkg/libgm/session_handler.go manages request IDs and responses, and pkg/libgm/http.go handles the actual HTTP requests, including adding authentication headers and cookies.
- If the session expires, the bridge will attempt to re-authenticate with the Google Messages servers.
- The
refreshAuthToken method in pkg/libgm/client.go handles refreshing auth tokens if they expire. The bridge handles both Tachyon auth and Google auth via cookies.
5. Event Handling:
- The bridge has an event handling system (
pkg/libgm/event_handler.go) that processes incoming messages, decrypts them, and triggers relevant events to be handled by the connector package.
- This is how the bridge is notified of new messages, read receipts, etc. even when the app is backgrounded.
In Summary:
The key to this bridge working in the background is the long-polling connection combined with the ditto pinger and data receive check. These mechanisms allow the bridge to receive updates from the Google Messages service even when the app is not active. The session handling and re-authentication mechanisms make sure that the connection is maintained and can be recovered when necessary.
Things to check in your implementation:
- Long Polling Implementation: Make sure you have implemented long polling correctly and you're not closing the connection too early.
- Keep-Alive Mechanism: Implement a similar pinging mechanism to keep the connection alive.
- Authentication/Session Management: Properly store, manage, and refresh your session credentials.
- Error Handling: Gracefully handle connection errors and implement reconnection logic.
- Message Deduplication: The bridge deduplicates messages using a hash of the message data. Implement something similar to avoid processing duplicate messages.
- Message Acks: Make sure you are properly sending message acks to the server so that the server knows you've received the messages.
By implementing these mechanisms, you should be able to make your Google Messages bridge function reliably, even when the app is in the background.