Skip to content

About

Multiplayer P2P Snake Game on Pear for Mobile

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Snake (mobile)

P2P multiplayer snake for iOS and Android — the mobile equivalent of snake (desktop), built on pear-mobile with the hello-pear-react-native boilerplate.

Same game, same wire protocol, same board as the desktop build — a mobile peer and a desktop peer can play together on the same topic. Only the shell changes: an Expo / React Native app instead of Electron, touch controls instead of a keyboard, and pear-mobile instead of pear-runtime.

Architecture

The app runs as a standard Expo / React Native application. Peer-to-peer networking via Hyperswarm runs inside an embedded Bare worklet spawned by pear-mobile — keeping Node/Bare APIs out of the React Native JS thread. The view communicates with the worker over a framed JSON IPC stream.

React Native view (src/)
  └─ pear-mobile PearRuntime.run('/worker.bundle', …)
       └─ framed-stream IPC
            └─ bare worklet (workers/main.js)  ←→  Hyperswarm (P2P)

The game itself — snake movement, wrapping, collisions, food placement — lives entirely in the view layer (src/game/engine.ts), a port of the desktop renderer's logic with the browser bits (canvas, keydown, custom element) removed. The worker is only P2P transport plus OTA updates. The worker is kept in-app (workers/main.js) rather than sharing the hello-pear-worker package.

IPC protocol (identical to the desktop build)

Direction Message Meaning
view → worker { type: 'join', topic } Join or create a game (null topic = create)
view → worker { type: 'send', data } Broadcast game state to peers
view → worker { type: 'applyUpdate' } Apply a downloaded OTA update
worker → view { type: 'ready', id, topic } Swarm flushed, game can start
worker → view { type: 'connected', id } Peer joined
worker → view { type: 'disconnected', id } Peer dropped
worker → view { type: 'data', id, payload } Game state from a peer
worker → view { type: 'update', connections } Peer count changed
worker → view { type: 'updating' } An OTA update is downloading
worker → view { type: 'updated' } An OTA update is ready to apply
worker → view { type: 'updateApplied' } Reply after an update was applied

Controls

  • Swipe anywhere on the board to steer, or use the on-screen D-pad. Both map to the desktop arrow keys — you cannot reverse directly onto your current axis.
  • Create starts a new game and shows a topic to share. Join connects to a topic someone shared with you.

Requirements

  • npm
  • node --version >= 20.19.4
  • iOS Simulator: xcodebuild -version >= 26.2, iOS >= 15.1
  • Android Simulator: Android >= 10 (react-native-bare-kit)

See Expo SDK 55 for the full support matrix.

Development

npm install

Bundle the Bare worker into a worklet (required before the first run and after any change to workers/main.js):

npm run bundle:bare

Run on a simulator/device:

npm run ios
# or
npm run android

Use npm start to (re)start just the Metro bundler without rebuilding. npm run ios and npm start use Metro port 8099. A dev build loads its JS from Metro; a release build (npm run production:ios) embeds the bundle and ignores Metro.

In development (npm run ios / npm run android) the app passes __DEV__ to the worker so OTA updates are disabled, mirroring the desktop --no-updates default.

Releases

holepunchto/pear-snake-ci-build runs this app's build scripts without signing credentials, then signs and publishes the artifacts in separate jobs using its protected release environment. This repository keeps app build configuration and development commands; signing and publishing scripts live in CI. See Releasing for setup.

After native prebuild (and pod install for iOS), npm run build:ios and npm run build:android compile unsigned release artifacts into dist/ios and dist/android. The Android command requires Gradle 9.

OTA updates & deploy

Applying an update reloads the app after the worker applies it. pear-runtime-react-native selects the latest eligible Pear bundle on iOS and Android. Its Expo plugin generates the Android reload hook in modules/pear-runtime-reload; this directory is ignored by Git and recreated during prebuild. Installing the dependency alone does not add the hook to an existing Android binary: run npm run prebuild, then build and install the native app. An unavailable or failed reload, or one that leaves the app running for 10 seconds, shows instructions to fully stop and reopen the app.

OTA updates require a newer package.json version than both the native app and the currently running JavaScript bundle. Increasing only the build number is insufficient.

npm run update   # bundle:bare + bundle:react-native + build → dist/

Then stage, seed, provision and (for production) multisign the dist/ folder against the upgrade link in package.json.

Before a production build, set package.json upgrade to a real pear:// link (pear touch) and bump version.

License

Apache-2.0

About

Multiplayer P2P Snake Game on Pear for Mobile

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages