パッケージの詳細

@laravel/echo-react

laravel6.2kMIT2.1.6

React hooks for seamless integration with Laravel Echo.

laravel, pusher, ably, react

readme

Laravel Echo React Helpers

configureEcho

You must call this function somewhere in your app before you use useEcho in a component to configure your Echo instance. You only need to pass the required data:

import { configureEcho } from "@laravel/echo-react";

configureEcho({
    broadcaster: "reverb",
});

Based on your brodcaster, the package will fill in appropriate defaults for the rest of the config based on the Echo documentation. You can always override these values by simply passing in your own.

In the above example, the configuration would also fill in the following keys if they aren't present:

{
    key: import.meta.env.VITE_REVERB_APP_KEY,
    wsHost: import.meta.env.VITE_REVERB_HOST,
    wsPort: import.meta.env.VITE_REVERB_PORT,
    wssPort: import.meta.env.VITE_REVERB_PORT,
    forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
    enabledTransports: ['ws', 'wss'],
}

useEcho Hook

Connect to private channel:

import { useEcho } from "@laravel/echo-react";

const { leaveChannel, leave, stopListening, listen } = useEcho(
    `orders.${orderId}`,
    "OrderShipmentStatusUpdated",
    (e) => {
        console.log(e.order);
    },
);

// Stop listening without leaving channel
stopListening();

// Start listening again
listen();

// Leave channel
leaveChannel();

// Leave a channel and also its associated private and presence channels
leave();

Multiple events:

useEcho(
    `orders.${orderId}`,
    ["OrderShipmentStatusUpdated", "OrderShipped"],
    (e) => {
        console.log(e.order);
    },
);

Specify shape of payload data:

type OrderData = {
    order: {
        id: number;
        user: {
            id: number;
            name: string;
        };
        created_at: string;
    };
};

useEcho<OrderData>(`orders.${orderId}`, "OrderShipmentStatusUpdated", (e) => {
    console.log(e.order.id);
    console.log(e.order.user.id);
});

Connect to public channel:

useEchoPublic("posts", "PostPublished", (e) => {
    console.log(e.post);
});

Connect to presence channel:

useEchoPresence("posts", "PostPublished", (e) => {
    console.log(e.post);
});

Listening for model events:

useEchoModel("App.Models.User", userId, ["UserCreated", "UserUpdated"], (e) => {
    console.log(e.model);
});

更新履歴

Release Notes

Unreleased

v1.17.1 - 2024-11-26

v1.17.0 - 2024-11-12

v1.16.1 - 2024-04-09

v1.16.0 - 2024-02-20

v1.15.3 - 2023-08-29

v1.15.2 - 2023-07-11

v1.15.1 - 2023-04-26

v1.15.0 - 2023-01-17

Added

v1.14.2 - 2022-11-22

Fixed

v1.14.1 - 2022-10-25

Fixed

v1.14.0 - 2022-08-30

Added

v1.13.1 - 2022-08-02

Changed

v1.13.0 - 2022-07-26

Added

v1.12.1 - 2022-07-13

Fixed

v1.12.0 - 2022-06-21

Added

v1.11.7 - 2022-04-21

No significant changes.

v1.11.6 - 2022-04-21

No significant changes.

v1.11.5 - 2022-04-12

Changed

v1.11.4 - 2022-03-15

Changed

v1.11.3 (2021-10-26)

Fixed

  • Fix types in connector.ts (#327)

v1.11.2 (2021-08-31)

Changed

  • Package build

v1.11.1 (2021-08-03)

Changed

  • Extend presence channel by channel class (#318)

v1.11.0 (2021-06-17)

Added

  • Add listenToAll and stopListeningToAll (#315)

v1.10.0 (2020-12-19)

Added

  • Add optional callback argument to stopListening() (#292)

v1.9.0 (2020-10-13)

Added

  • Register subscription succeeded callbacks (#288)

v1.8.1 (2020-07-31)

Added

  • Implement error handling with support for Pusher (#284)

v1.8.0 (2020-05-16)

Fixed

  • IE11 fix for dist/echo.js not being transpiled to ES5 (#270)

v1.7.0 (2020-04-03)

Added

  • Add pusher private-encrypted (#264)

v1.6.1 (2019-10-01)

Fixed

  • Change check for 'querySelector' method for browser compatibility (#253)

v1.6.0 (2019-09-24)

Added

  • Add stopWhisper method to channel (#243)
  • Add option this.options.withoutInterceptors (#248)
  • Add support for custom connectors (#247)

Fixed

  • Check for querySelector (#251)

v1.5.4 (2019-06-14)

Fixed

  • Ensure passed param wins over global (#235)

v1.5.3 (2019-02-14)

Fixed

  • Add reference to TS declaration file (#222)
  • Add missing method to Echo instance (fd3b65b)

v1.5.2 (2018-12-12)

Added

  • Add iife output to dist (#214)
  • Add leaveOne method to connectors (#216, 9809405)

v1.5.1 (2018-12-05)

Added

  • Add commonjs output to distribution (#212)

v1.5.0 (2018-12-01)

Changed

  • General maintenance, code styling, and add stopListening() for socket.io (#210)