搜尋套件、儲存庫與 APP 分類

laravel_echo 封面
套件詳情v0.2.9

laravel_echo

適用於您 Flutter 應用的官方 Laravel Echo 套件移植。

近 30 天下載
850
喜歡數
68
Pub 分數
30

安裝套件

flutter pub add laravel_echo

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter/Dart 套件,將 Laravel Echo 的功能帶到行動應用程式中,透過 WebSockets 實現即時通訊。它讓 Flutter 應用程式能夠監聽由 Laravel 驅動的後端事件,例如通知、聊天訊息或即時更新,直接從伺服器取得。此套件專為與 Laravel 的廣播系統無縫整合而設計,支援頻道及私人/公開事件訂閱。對於使用 Laravel 作為後端建構 Flutter 應用程式即時功能的開發者而言,這是一個理想的選擇。

適用情境

  • 即時聊天應用
  • 即時通知系統
  • 事件驅動的UI更新
  • Laravel 後端整合
  • 協作式網頁應用功能

主要特色

  • 基於WebSocket的即時通訊
  • 支援私人和公開頻道
  • 與 Laravel 廣播的整合
  • 事件監聽器回呼
  • 簡單的訂閱管理

適合對象

  • Flutter 應用與 Laravel 後端
  • 熟悉 Laravel Echo 的開發者
  • 即時功能實現
  • 全-stack Flutter 和 Laravel 項目
  • 需要即時資料同步的應用程式

使用建議

  • 需要一個配置了廣播功能的 Laravel 後端
  • 僅限 WebSocket 傳輸
  • 無內建回退機制
  • 依賴外部伺服器設定
  • 與平台無關,但需要網路連接

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Laravel Echo for Flutter

Basically, this package is port of official Laravel Echo javascript library. It helps subscribe to channels and listen for events broadcasted from your Laravel app.

API is mostly same as official Echo package, so everything in Official documentation should work.

Available connectors:

Screenshot

Getting started

Pusher

To use with Pusher, you need to install pusher_client for you Flutter app.

In your pubspec.yaml file:

dependencies:
  ...
  pusher_client: ^2.0.0
  laravel_echo: ^1.0.0

import laravel_echo and pusher_client

import 'package:laravel_echo/laravel_echo.dart';
import 'package:pusher_client/pusher_client.dart';
const String PUSHER_KEY = 'YOUR_PUSHER_KEY_HERE';
const String PUSHER_CLUSTER = 'YOUR_PUSHER_CLUSTER_HERE';
const String BEARER_TOKEN = 'YOUR_BEARER_TOKEN_HERE';
const String AUTH_URL = 'http://echo.test/api/broadcasting/auth';

PusherOptions options = PusherOptions(
  cluster: PUSHER_CLUSTER,
  auth: PusherAuth(
    AUTH_URL,
    headers: {
      'Authorization': 'Bearer $BEARER_TOKEN',
    },
  ),
);

// Create pusher client
PusherClient pusherClient = PusherClient(
  PUSHER_KEY,
  options,
  autoConnect: false,
  enableLogging: true,
);

// Create echo instance
Echo echo = new Echo(
  broadcaster: EchoBroadcasterType.Pusher,
  client: pusherClient,
);

// Listening public channel
echo.channel('public-channel').listen('PublicEvent', (e) {
  print(e);
});

// Accessing pusher instance
echo.connector.pusher.onConnectionStateChange((state) {
  print(state!.currentState.toString());
});
socket.io

To use with socket.io, you need to install socket_io_client for your Flutter app.

In your pubspec.yaml file:

dependencies:
  ...
  socket_io_client: ^1.0.1
  laravel_echo: ^1.0.0

import socket_io_client

import 'package:laravel_echo/laravel_echo.dart';
import 'package:socket_io_client/socket_io_client.dart' as IO;

usage

const String BEARER_TOKEN = 'YOUR_BEARER_TOKEN_HERE';

// Create socket.io client
IO.Socket socket = IO.io(
  'http://localhost:6001',
  IO.OptionBuilder()
      .disableAutoConnect()
      .setTransports(['websocket']).build(),
);

// Create echo instance
Echo echo = new Echo(
  broadcaster: EchoBroadcasterType.SocketIO,
  client: socket,
  options: {
    'auth': {
      'headers': {
        'Authorization': 'Bearer $BEARER_TOKEN',
      }
    },
  },
);

// Listening public channel
echo.channel('public-channel').listen('PublicEvent', (e) {
  print(e);
});

// Listening private channel
// Needs auth. See details how to authorize channel below in guides
echo.private('private-channel').listen('PrivateEvent', (e) {
  print(e);
});

// Listening presence channel
// Needs auth. See details how to authorize channel below in guides
echo.join('presence-channel')
  .here((users) {
    print(users);
  }).joining((user) {
    print(user);
  }).leaving((user) {
    print(user);
  }).listen('PresenceEvent', (e) {
    print(e);
  });

// Accessing socket instance
echo.connector.socket.onConnect((_) => print('connected'));
echo.connector.socket.onDisconnect((_) => print('disconnected'));

Guide

Options
Option Description Default
auth
authEndpoint /api/broadcasting/auth
broadcaster EchoBroadcasterType.Pusher
host Socket host http://localhost:6001
namespace Event namespace App.Events
... Any other options, passed as socket params

Sample backend

Backend used for example app could be found at echo-server

Credits

This package would not be existed without hard work of these awesome devs.

  • @chinloyal - maintainer of pusher_client
  • @rikolu - maintainer of socket_io_client
  • @heywhy - for initial Pusher intagration

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。