0.3.0 • Published 4 months ago

api-sync-robojs v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 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

4 months ago

0.2.2

4 months ago

0.2.1

5 months ago

0.2.0

5 months ago

0.1.10

7 months ago

0.1.10-beta-1

7 months ago

0.1.8

8 months ago

0.1.9

7 months ago

0.1.7

8 months ago

0.1.6

8 months ago

0.1.5

8 months ago

0.1.4

8 months ago

0.1.3

8 months ago

0.1.2

8 months ago

0.1.1

8 months ago

0.1.0

8 months ago