Some thoughts on supporting mobile

:wave:
After giving RedwoodJS a shot in a personal project, it got me excited and made me think about the possibility of RedwoodJS having support for building mobile apps too. I think the idea is having one command, yarn rw dev, that runs the API and the Web is awesome and could be extended to include mobile too.

This topic is a brain-dump around that idea. If it aligns with RedwoodJS long-term plans I’ll be happy to contribute this feature.

1. Zero-config

Like RedwoodJS does for web and API, I think the configuration required for underlying tools such as the Metro bundler should be abstracted away. Users would have a mobile directory with the sources:

mobile/
├── js/
│   └── App.tsx
├── android/
│   ├── Foo.kotlin
└── ios/
    └── Bar.swift

RedwoodJS could be opinionated about where the native iOS and Android code and resources live, as well as how the Javascript-side (React) is structured.

2. Network and navigation

import { useQuery, NavigationContainer  } from '@redwoodjs/mobile'

Two building blocks that most apps will need are navigation and networking. In the case of networking, mobile apps could reuse the same stack used for the web (unless there are dependencies with browser-specific APIs). In the case of navigation, RedwoodJS could wrap the react-native-navigation. It’s a widely used and extensively-tested library so I don’t think it should re-invent the wheel with a new implementation.

The equivalent of the navigation primitives for Web would be the router.

3. Auto-linking

Auto-linking and the build pipeline, which are required for building the native apps, are provided by the community’s react-native-cli. I think re-using that from RedwoodJS would worsen the great user-experience that RedwoodJS provides. The reason being is that react-native-cli has an implicit dependency with CocoaPods (Ruby) in order to link the native code of React Native dependencies pulled by NPM/Yarn.
Adding a dependency between RedwoodJS and the React Native CLI would add Ruby & CocoaPods as a transitive dependency and it’d require RedwoodJS users to install Ruby & CocoaPods as well.

I propose building a pure Javascript utility that acts as Webpack but for React Native project. It’d have three responsibilities:

  • Auto-link: Without depending on CocoaPods. In the case of iOS, it’d generate Xcode projects with the right linking settings (I have a lot of experience with this one :grimacing:), and in case of Android, we’d provide a Gradle task that resolves the dependencies at load time.
  • Build pipeline: The Android and iOS projects would trigger Metro through this component. It’d make sure that we are calling Metro with the right configuration.
  • Passing variables: That can be accessed from React Native. We could provide components along the lines of useAuth, to access variables that are passed by RedwoodJS. For example:
import { useVersion } from "@redwoodj/mobile"

Note that the tool would make Xcode and Android projects an implementation detail. Unlike react-native-cli that keeps those in the repository, and thus complicating RN upgrades, projects are generated when needed. For example, let’s say the user wants to work on the iOS app. They could run yarn rw dev ios, and the following would happen behind the scenes:

  1. Generate a build.gradle or App.xcodeproj in a .gitignored directory.
  2. Compile the app using xcodebuild (iOS), or gradle (Android)
  3. Launch the simulator (iOS) / emulator (Android) with the app and the bundler.
  4. Run the API.

Since RedwoodJS knows that the app is running in development, it’d pass the right variable to the build pipeline so that internally the network stack points to the right IP address.

4. Shared components

The introduction of mobile, creates the opportunity for sharing components between mobile and the web. There are libraries like react-native-web that provide cross-platform components. RedwoodJS users could reuse those, or implement new ones under a shared/ directory at the root of the project:

shared/
└── components/
    └── MyButton.jsx

Final thoughts

Since working on the component that provides auto-linking and a build-pipeline without the CocoaPods dependency has been in my mind for a long time, I’ll start working on that. I think the official react-native-cli could leverage this implementation, and also RedwoodJS if they decide to include mobile in the scope of the project.

10 Likes

Adding one more thought

Standard authentication

I think the same approach that is being used for the web would work for mobile too with some adjustments. The redirects would need to be done using an embedded web view, and the JWT token would have to be persisted using Keychain (iOS), and the secure storage (Android). What’s great about it is that it’d be the same API for the user.

1 Like

@pepibumur Thank you for this! Definitely :heart: it all and want to help you build up some momentum.

  1. Have you seen the Redwood concept of “Sides”?
  2. Regarding React Native POC, @vilvaathibanpb got a start on this work:

However, we decided to not work on officially supporting Sides until post the release of Redwood v1.0. That doesn’t mean work on this has to wait until then. And, if you’re up for it, we’d like to help if/where we can to support you moving something forward.

Looping in @peterp --> do you see anything here that’s near-term actionable for a simple next step?

Lastly, Pedro, do you have more tangible specifics about what “If it aligns with RedwoodJS long-term plans I’ll be happy to contribute this feature” would look like in order to help you take a next step?

1 Like

:wave:
Hey @thedavid, sorry, I missed this message. Let me answer some of your points.

Have you seen the Redwood concept of “Sides”?

Yes, and I have to say it’s a great idea. As a developer, it’s a great experience being able to run one command and getting both sides running and interacting with each other (e.g. React frontend interacting with the GraphQL API). Some teams have Shopify have started using React for their web apps, and the integration between a Rails and the Webpack process is not as seamless as it could be.

However, we decided to not work on officially supporting Sides until post the release of Redwood v1.0. That doesn’t mean work on this has to wait until then. And, if you’re up for it, we’d like to help if/where we can to support you moving something forward.

I’d love to give it a shot. I started using Redwood for a personal project for which I’d like to include a mobile app, and it feels a bit odd having to manage the “mobile” side myself.

Lastly, Pedro, do you have more tangible specifics about what “If it aligns with RedwoodJS long-term plans I’ll be happy to contribute this feature” would look like in order to help you take the next step?

I can hack something to get a sense of how the experience looks for the user and how it integrates with the existing foundation. I’ll take me some time to op

FYI, I opened an RFC on React Native’s repository to remove some indirection and inconveniences that would surface if RedwoodJS adds the mobile side to the framework. Hopefully, I’ll be able to work on it and get it in a good shape before integrating it with RedwoodJS.

6 Likes

I don’t have anything constructive to add here just wanted to say thank you for laying this groundwork, lots of people are excited about getting Redwood working with mobile but there hasn’t been a ton of work put into it yet.

Quick update --> @pepibumur and I connected about him diving in and taking some next steps, which is definitely :rocket:

@peterp nothing urgent for you todo list right now, but in the near-term it would be helpful for you and I to consolidate the latest thoughts on how to both define Sides as well as how to structure config, etc. (e.g. what are the implications). I told Pedro to reach out anytime and as needed.

@aldonline wanted to put this on your radar as well. Nothing for you to do at this time, I just know you’re deep in the structure package and will have some thoughts about implications as well as ways forward.

Thanks, all! Excited for this POC!!

1 Like

If adding a native (or even multiple native sides based on the device) is going to

I’d very much like to see how one might add multiple web sides to the overall structure such that one could include multiple (in this case the RW React-based web sides) to deploy multiple sites that:

  • share an api (and thus is ok to access, modify data on that same api’s database/stores)
  • even possibly share components or cells
  • but then deploy to separate sites (different deployed domains or deployed to different subdomains).

A few core usecases would be:

But in each they could share the api and some components.

3 Likes

Is there any progress or recommendations to achieve this kind of separation?

We have the exact use case outline above and would like to deploy to different domains/environments.

There has been some more talk about mobile here

Thanks Tobbe.

Our concern is more about having multiple web apps (marketing, client, admin) than mobile, though a mobile app is also a possibility.

The Redwood monorepo is somehow strict on the list of sides (api, web). I know this can be partially worked around but having some guidance from the core team would be welcomed.

1 Like

Thanks for your clarification. My current thoughts on how to implement our mobile side it to basically make it another “web” side, but one that integrates Expo and ReactNative Web. And when/if we make it possible to have two web sides I’ll make sure to keep in mind your wishes (and, also my own) of making it generic enough to support any number of web sides.

Is there a quick TLDR on the summary of what’s blocking the path forward to supporting many sides? They’re essentially UIs, right? The idea being that the API is shared amongst them all? Seems sensible. It’s not really following a microservices style pattern for the APIs, which i think is fine since that’s kind of the point of GraphQL, unless I’m totally way off. Is it just the isomorphic type sharing stuff, or is there more to it than that?

Yes, the idea is that the API is shared.

The main blocker is that we want to finish our work on moving to Vite and enabling SSR first.

1 Like