π@dpapi/react
Bring multiplayer to your React JS app
Install Package
npm install --save @dpapi/reactRoomProvider
A room is the space where people can join and start collaborating together.
RoomProvider is used to create context and make it available to all child components in the React component tree hierarchy. This provides a socket connection to all components that are wrapped in it.
import React from "react";
import ReactDOM from "react-dom";
import {RoomProvider} from "@dpapi/react";
const rootElement = document.getElementById("root");
ReactDOM.render(
<RoomProvider>
<App />
</RoomProvider>,
roomElement
);
clientSocket
clientSocket is to declare the socket connection that is passed down to all child components within the room. This connection is further used to bring the multiplayer functionalities in this room.
Create a socket instance by passing clientSocket in useContext.
joinRoom
Function that enables a client to join the virtual room. Automatically communicate updates to the current userβs presence and cursor movements.
This function takes two arguments - room-id and socket connection.
Ensure that joinRoom function is called before the other multiplayer functionalities of roomCount and liveCursors.
However, you will need to initiate the clientSocket connection before joining a room.
roomCount
Function used to subscribe to the presence of other users in the room.
This function takes two arguments - room-id and socket connection.
liveCursors
Function to set and update cursor movements of all users in the room.
This function takes two arguments - room-id and socket connection.
Your app is now multiplayer π
Last updated