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

feat: @farcaster/js should run in browsers #573

Closed
varunsrin opened this issue Feb 15, 2023 · 6 comments · Fixed by #856
Closed

feat: @farcaster/js should run in browsers #573

varunsrin opened this issue Feb 15, 2023 · 6 comments · Fixed by #856
Assignees
Labels
s-ready Ready to be picked up t-devex Simplify the developer experience
Milestone

Comments

@varunsrin
Copy link
Member

What is the feature you would like to implement?

Get @farcaster/js compiling in browsers

Why is this feature important?

Single-page and stand alone client applications will be able to interface directly with Hubs without a server.

Will the protocol spec need to be updated??

No

Additional context

  • Can @farcaster/js just use grpc-web instead?
  • Can @farcaster/hubble run a server that runs both grpc-js and grpc-web?
  • Can @farcaster/js compile in browsers end to end?
@varunsrin varunsrin added the t-feat Add a new feature or protocol improvement label Feb 15, 2023
@varunsrin varunsrin added this to the v2.0.0 p2 milestone Feb 15, 2023
@pfletcherhill
Copy link
Contributor

@kcchu
Copy link
Contributor

kcchu commented Feb 18, 2023

My two cents:

  1. I am uncertain about the use case of calling Hub RPC from web browsers. The RPC methods are primitive compare with Merkle API (e.g. Hub RPC don't handle threading). A web browser client couldn't do much thing without talking to a higher-level API server (e.g. Merkle API, Farcaster Indexer).
  2. Client-Hub communication makes sense if the plan is to implement most functionalities needed by a client in Hub RPC.
  3. And alternative approach to (2) is releasing a separated Farcaster API services. Hub remains as a service that is primarily for distributing messages and syncing with the peer-to-peer network. The Farcaster API talks to a Hub and expose higher level API for clients (it may implement add-on functions on top of Hub like searching).
  4. Even if grpc-web is to be implemented in Hub, RPC between Hubs should stay on standard gRPC because it supports the most programming languages and much more efficient.
  5. There is no Typescript implementation of grpc-web proxy. A solution is embedding a Envoy proxy in the docker image to serve the grpc-web port.

@pfletcherhill
Copy link
Contributor

Supporting web browsers isn't an immediate problem, but in the medium term it lowers the barrier to entry for community members looking to make something using farcaster, which is good. I imagine some devs will want very basic access and choose to use the hub RPC for reading/writing and others will use more feature-rich APIs or build their own backends for indexing data. There's certainly an opportunity for community members to make their own indexing services and APIs on top of hubs for many use cases, similar to Alchemy's various APIs for ethereum.

We should support both grpc and grpc-web simultaneously using a proxy (I made it work with Envoy, as you mentioned, though there may be other options too). So hub-to-hub communication will continue to use gRPC directly.

@varunsrin
Copy link
Member Author

we should see whether farcaster/js works by default in serverless platforms like vercel. if it does, we can punt this to a later milestone

@varunsrin varunsrin modified the milestones: v2.0.0 p3, v2.0.0 p4, vNext Mar 13, 2023
@pfletcherhill
Copy link
Contributor

pfletcherhill commented Mar 30, 2023

High-level goal:

Create a new package called @farcaster/hub-web that exports the same methods as @farcaster/hub-nodejs except with a grpc-web client instead of a grpc one that works in browsers.

Work to be done:

  • Create new @farcaster/hub-web package in the monorepo
  • Generate grpc-web compatible client from the existing rpc.proto schema and export it from the new package. If using ts-proto, we can use the outputClientImpl=grpc-web option (see documentation).
  • Wrap the grpc-web client implementation in promises and results (see client.ts in utils for reference)
  • We want to re-export all of @farcaster/utils from the new package, but utils right now depends on @farcaster/protobufs which depends on @grpc/grpc-js which is not supported in the browser. So we need to refactor utils to not depend on grpc-js. One option would be to remove any services generated code from the protobufs package (see the outputServices=false ts-proto option) and add generated grpc-js code to the @farcaster/hub-nodejs package. That way the nodejs package has the grpc-js client and the web package has the grpc-web client.
  • Refactor the hubble app to either use the @farcaster/hub-nodejs package or generate its own server and client grpc-js implementations from the rpc.proto schema, since right now those are imported from the protobufs package (see server.ts and hubble.ts).
  • Bonus points for merging @farcaster/utils and @farcaster/protobufs into a single @farcaster/core repo, since they'll always be used together and should support all environments after the refactor.
  • Further bonus points for developing a nice pattern for sharing protobufs schemas between packages in a monorepo.
  • Add a grpc-web proxy to the hub deployment. See grpc-web docs for envoy example.

@dawsbot
Copy link
Contributor

dawsbot commented Apr 4, 2024

It appears this is solved, yet the readme states that it is not

I've supplied a PR to remove that! Let me know if this looks right 🙏 #1865

adityapk00 pushed a commit that referenced this issue Apr 4, 2024
## Motivation

Browser support was added via
#573 but this
documentation was not updated to reflex those changes

## Change Summary

See above ^

## Merge Checklist

_Choose all relevant options below by adding an `x` now or at any time
before submitting for review_

- [x] PR title adheres to the [conventional
commits](https://www.conventionalcommits.org/en/v1.0.0/) standard
- [ ] PR has a
[changeset](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#35-adding-changesets)
- [ ] PR has been tagged with a change label(s) (i.e. documentation,
feature, bugfix, or chore)
- [ ] PR includes
[documentation](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#32-writing-docs)
if necessary.
- [x] All [commits have been
signed](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#22-signing-commits)


<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on updating the README.md file in the hub-nodejs
package. It includes changes related to error handling, required
packages, and support for both Nodejs and browser environments.

### Detailed summary
- Updated support information for Nodejs and browser environments.
- Improved error handling with a monadic pattern.
- Clarified the required packages for creating new messages.
- Encoded fixed length data in byte formats for consistency.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s-ready Ready to be picked up t-devex Simplify the developer experience
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants