Get started - Get started with Liveblocks and Redux

Liveblocks is a real-time collaboration infrastructure for building performant collaborative experiences. Follow the following steps to start making your Redux state multiplayer by using the store enhancer from the @liveblocks/redux package.

Quickstart

  1. Install Liveblocks

    $npm install @liveblocks/client @liveblocks/redux
  2. Connect your Redux store to Liveblocks

    Create the Liveblocks client and use the liveblocksEnhancer in your Redux store setup. This will add a new state called liveblocks to your store, enabling you to interact with our Presence and Storage APIs.

    store.ts
    "use client";
    import { createClient } from "@liveblocks/client";import { liveblocksEnhancer } from "@liveblocks/redux";import { configureStore, createSlice } from "@reduxjs/toolkit";
    const client = createClient({ publicApiKey: "pk_prod_xxxxxxxxxxxxxxxxxxxxxxxx",});
    const initialState = {};
    const slice = createSlice({ name: "state", initialState, reducers: { /* logic will be added here */ },});
    function makeStore() { return configureStore({ reducer: slice.reducer, enhancers: [ liveblocksEnhancer({ client, }), ], });}
    const store = makeStore();
    export default store;
  3. Join a Liveblocks room

    Liveblocks uses the concept of rooms, separate virtual spaces where people collaborate. To create a real-time experience, multiple users must be connected to the same room.

    App.tsx
    "use client";
    import { useEffect } from "react";import { useDispatch } from "react-redux";import { actions } from "@liveblocks/redux";
    export default function App() { const dispatch = useDispatch();
    useEffect(() => { dispatch(actions.enterRoom("room-id"));
    return () => { dispatch(actions.leaveRoom("room-id")); }; }, [dispatch]);
    return <Room />;}
  4. Use the Liveblocks data from the store

    Now that we’re connected to a room, we can start using the Liveblocks data from the Redux store.

    Room.tsx
    "use client";
    import { useSelector } from "react-redux";
    export function Room() { const others = useSelector((state) => state.liveblocks.others); const userCount = others.length; return <div>There are {userCount} other user(s) online</div>;}
  5. Next: set up authentication

    By default, Liveblocks is configured to work without an authentication endpoint where everyone automatically has access to rooms. This approach is great for prototyping and marketing pages where setting up your own security isn’t always required. If you want to limit access to a room for certain users, you’ll need to set up an authentication endpoint to enable permissions.

    Set up authentication

What to read next

Congratulations! You now have set up the foundation to start building collaborative experiences for your Redux store.


Examples using Redux