> For the complete documentation index, see [llms.txt](https://dpapi.gitbook.io/docs-dpapi-mutliplayer/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dpapi.gitbook.io/docs-dpapi-mutliplayer/api-reference/dpapi-react.md).

# @dpapi/react

### Install Package

```javascript
npm install --save @dpapi/react
```

### RoomProvider

A room is the space where people can join and start collaborating together.

<mark style="color:purple;">`RoomProvider`</mark> 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.

<pre class="language-javascript" data-title="Index.js" data-line-numbers><code class="lang-javascript">import React from "react";
import ReactDOM from "react-dom";
<strong>import {RoomProvider} from "@dpapi/react";
</strong>
const rootElement = document.getElementById("root");

ReactDOM.render(
<strong>    &#x3C;RoomProvider>
</strong>        &#x3C;App />
<strong>    &#x3C;/RoomProvider>, 
</strong>    roomElement
);

</code></pre>

### clientSocket

<mark style="color:purple;">`clientSocket`</mark> 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 <mark style="color:purple;">`clientSocket`</mark> in <mark style="color:purple;">`useContext`</mark>.

<pre class="language-javascript" data-title="App.js" data-line-numbers><code class="lang-javascript"><strong>import {useContext} from "react";
</strong><strong>import {clientSocket} from "@dpapi/react";
</strong>
function App() {
<strong>  const socket = useContext(clientSocket);
</strong>  // 
  // Add your JavaScript code here 👈
  //
  return (
    // Add your JSX / HTML code to render 👈
  );
}

export default App;
</code></pre>

### 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 - <mark style="color:purple;">`room-id`</mark> and <mark style="color:purple;">`socket`</mark> connection.

<pre class="language-javascript" data-title="App.js" data-line-numbers><code class="lang-javascript">import {useContext} from "react";
<strong>import {clientSocket, joinRoom} from "@dpapi/react";
</strong>
function App() {
  const socket = useContext(clientSocket);
<strong>  joinRoom("room-id",socket);
</strong>  // 
  // Add your JavaScript code here 👈
  //
  return (
    // Add your JSX / HTML code to render 👈
  );
}

export default App;
</code></pre>

{% hint style="warning" %}
Ensure that <mark style="color:orange;">`joinRoom`</mark> function is called before the other multiplayer functionalities of <mark style="color:orange;">`roomCount`</mark> and <mark style="color:orange;">`liveCursors`</mark>.

However, you will need to initiate the clientSocket connection before joining a room.
{% endhint %}

### roomCount

Function used to subscribe to the presence of other users in the room.&#x20;

This function takes two arguments - <mark style="color:purple;">`room-id`</mark> and <mark style="color:purple;">`socket`</mark> connection.

<pre class="language-javascript" data-title="App.js" data-line-numbers><code class="lang-javascript">import {useContext} from "react";
<strong>import {clientSocket, joinRoom, roomCount} from "@dpapi/react";
</strong>
function App() {
  const socket = useContext(clientSocket);
  joinRoom("room-id",socket);
<strong>  const userCount = roomCount("room-id",socket);
</strong>  // 
  // Add your JavaScript code here 👈
  //
  return (
    &#x3C;div className="App"> // ❗ Remember to add a CSS file
<strong>      Number of users in the room: {usersCount}
</strong>    // Add your JSX / HTML code to render 👈
    &#x3C;/div>
  );
}

export default App;
</code></pre>

### liveCursors

Function to set and update cursor movements of all users in the room.

This function takes two arguments - <mark style="color:purple;">`room-id`</mark> and <mark style="color:purple;">`socket`</mark> connection.

<pre class="language-javascript" data-title="App.js" data-line-numbers><code class="lang-javascript">import {useContext} from "react";
<strong>import {clientSocket, joinRoom, liveCursors} from "@dpapi/react";
</strong>
function App() {
  const socket = useContext(clientSocket);
  joinRoom("room-id",socket);
<strong>  const cursors = liveCursors("room-id", socket);
</strong>  // 
  // Add your JavaScript code here 👈
  //
  return (
    &#x3C;div className="App"> // ❗ Remember to add a CSS file
<strong>      {cursors.map((cursor, index) => {
</strong><strong>        if (cursor.socketId !== socket.id) {
</strong><strong>          return (
</strong><strong>            &#x3C;div
</strong><strong>              key={cursor.socketId || index}
</strong><strong>              className="other-cursor" // ❗ Style your cursors with CSS
</strong><strong>              style={{
</strong><strong>                left: cursor.x,
</strong><strong>                top: cursor.y,
</strong><strong>              }}
</strong><strong>            />
</strong><strong>          );
</strong><strong>        } else {
</strong><strong>          return null;
</strong><strong>        }
</strong><strong>      })}
</strong>      // Add your JSX / HTML code to render 👈
    &#x3C;/div>
  );
}

export default App;
</code></pre>

> <mark style="color:blue;">**Your app is now multiplayer 🎉**</mark>
