0.3.0 • Published 6 months ago

api-sync-robojs v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

šŸš€ api-sync

Welcome to api-sync! Real-time state sync across clients and server the simplest way possible. Perfect for multiplayer games and chat apps. It's like magic, but real! šŸŽ©āœØ

āš ļø Under development; Not ready for production and subject to change.

New to Robo.js?

āžž šŸ“š Robo.js Documentation: Getting started

āžž šŸš€ Robo.js Community: Join Discord server

Installation

To add this plugin to your Robo.js project:

npx robo add api-sync-robojs

Note: You will also need to install the @robojs/server v1.6.1 or greater (@latest is recommended)

Usage šŸŽØ

Server

// src/events/_start.ts
import { SyncServer, Api, SyncState } from 'api-sync/server.js'

const myApi = {
	hello() {
		console.log('Hello from', this.id) // connection id
	},
	counter: new SyncState<number>()
} satisfies Api

export type MyApi = typeof myApi

export default async () => {
	SyncServer.defineApi(myApi)
}

Client

setup client api provider

// src/app/App.tsx
import { Activity } from './Activity'
import './App.css'
import { createApiClient } from 'api-sync/client.js'
import type { MyApi } from '../events/_start.js'
const { ApiContextProvider, useApi } = createApiClient<MyApi>()
export { useApi }

export default function App() {
	return (
		<DiscordContextProvider>
			<ApiContextProvider>
				<Activity />
			</ApiContextProvider>
		</DiscordContextProvider>
	)
}

use api

// src/app/Activity.tsx
import { useApi } from '.App'

export default function Activity() {
	const api = useApi()
	const counter = api.counter.use()

	useEffect(() => {
		api.hello()
	}, [])
}
0.3.0

6 months ago

0.2.2

6 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.1.10

10 months ago

0.1.10-beta-1

10 months ago

0.1.8

10 months ago

0.1.9

10 months ago

0.1.7

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago