Get startedGet started with Liveblocks and Redux

Liveblocks is a realtime 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.


  1. Install Liveblocks

    Every package should use the same version.

    $npm install @liveblocks/client @liveblocks/redux
  2. Initialize the liveblocks.config.ts file

    We can use this file later to define types for our application.

    $npx create-liveblocks-app@latest --init --framework react
  3. 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.

    "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;
  4. Join a Liveblocks room

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

    "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 />;}
  5. 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.

    "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>;}
  6. 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