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

Proposal for Connection(websocket, buetooth) management in the mobile app [w36] #787

Open
1 of 3 tasks
josancamon19 opened this issue Sep 8, 2024 · 6 comments
Open
1 of 3 tasks
Assignees

Comments

@josancamon19
Copy link
Contributor

josancamon19 commented Sep 8, 2024

Thinh here! So, what is this?

You know that our app (Omi) is a fast draft version. We focused on making it work first, but now we need to scale it, preparing for 100k users in the near future.

Let's focus on the most important part of Omi first: connection management. Why? Because the core of Omi is to connect to the world (of devices, services) — like Friend Necklace, OpenGlass, Frame, and much more.

Objective

  • Simplify the connection management in the app, making it stable.

Key Results

  • WebSocket connection must be stable and error-free.
  • Bluetooth connection must be stable and error-free.

How to solve it

I won’t dwell on how bad it currently is; instead, we’ll focus on how good it can be. I'll move quickly to make it happen on the Omi app ASAP.

A/ Principles

  1. Treat everything like a third-party service, even if it's your backend (Omi API) or your device (Friend).
  2. The number of connections must be limited, even if the limit is 1 million connections.
  3. Always handle connection errors, periodically or on-call, even if they claim there are no errors on their side.
  4. Use a service layer; don't directly communicate from the UI, even if you're using a state management framework (like Provider in Flutter).
  5. Simplify events when communicating with keep-alive services: Open, Close, Message, Error. That’s all.
  6. Error codes are your friend. Leverage them.
  7. Auto-reconnect with back-off exponential; don’t retry indefinitely. A dead state must be activated after hard retries.

B/ Implementation

  1. Core layers:

    • KeepAlivedConnection: The base class for WebSocketConnection, BluetoothConnection, and even InternetConnection.
      • open()
      • close()
      • read()
      • write()
      • subscribe(event, context)
      • unsubscribe(event, context)
    • KeepAlivedService(pool, connections)
    • KeepAlivedServiceManagement(pools): (KASM) follows the singleton pattern — only one instance at the global level. Its responsibility is to manage service instances (via pools) and the states between them. init() and dispose() must be called at the main level.
      • init()
      • dispose()
      • socket()
      • ble()
      • internet()
  2. How to use it?

    • In a state management framework like Provider, or even Widget
      • kasm.socket() -> to get a socket connection instance, then subscribe, read, write, unsubscribe.
      • kasm.ble() -> to get a BLE connection instance, then subscribe, read, write, unsubscribe.
      • kasm.internet() -> to subscribe the internet status changes
@beastoin
Copy link
Collaborator

beastoin commented Sep 8, 2024

Hi man, i am here! WIP, stay tuned?

@josancamon19
Copy link
Contributor Author

Looks solid, one thing here, let's have a metric of success

  1. on iOS to maximize the connectivity until iOS kills the app, metric of 3 hours seem reasonable, but not sure if makes sense
  2. on Android it should work the whole day without disconnection, cause in theory this should be doable.

@thinhx @mdmohsin7

@beastoin
Copy link
Collaborator

beastoin commented Sep 8, 2024

agreed!

@beastoin
Copy link
Collaborator

beastoin commented Sep 8, 2024

Week # 36

@beastoin beastoin changed the title Proposal for Connection(websocket, buetooth) management in the mobile app Proposal for Connection(websocket, buetooth) management in the mobile app [w36] Sep 8, 2024
@mdmohsin7
Copy link
Collaborator

A case to be considered:
When the app is in background and if the bluetooth permission is requested by the app, then it will result in this exception on Android. Only system apps can do this, third-party apps are not allowed to gain bluetooth permission without user intervention

https://dashboard.instabug.com/crashes/vL5FR_EExZlyXOj8tT-ivw

@beastoin
Copy link
Collaborator

beastoin commented Sep 17, 2024

Updates:

  1. What are we aiming to do ?
  • Rebuild the ble connectivity management app <> device
  • Rebuild the socket connectivity management app <> backend
  1. What we did ?
  • Rebuilt the ble connectivity management app <> device
    • BLE Connectivity is now stable, the status of Device <> App is synced.
    • iOS: workings for hours, tested in 6 hours.
    • Android: working for hours, in theory it should works whole day.
  1. Any risks / blockers ?
  • Frame device is still not tested yet.
  • the App is terminated by the OS, sometime!
  1. If we have more time, what would we do ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In review
Development

No branches or pull requests

3 participants