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.

    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.

    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.

    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. Bonus: set up authentication

    By default, Liveblocks is configured to work without an authentication endpoint. This approach is great for prototyping and marketing pages where defining your own security isn’t always required. If you want to implement your own security logic to define if certain users should have access to a given room, you’ll need to implement an authentication endpoint.

    Set up authentication

Next steps

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