Dart/Flutter client for Laravel Echo via Socket.IO.


To use this plugin, add flutter_laravel_echo_client as a dependency in your pubspec.yaml file.

Import the library.

import 'package:flutter_laravel_echo_client/laravel_echo_client.dart';

Usage

import 'package:flutter_laravel_echo_client/laravel_echo_client.dart';

final client = LaravelEchoSocketClient(
  SocketConfig(
    host: Uri.parse('https://echo.example.com'),
    token: '<ACCESS_TOKEN>',
    authHeadersProvider: () => {'Authorization': 'Bearer <ACCESS_TOKEN>'},
  ),
);

await client.connect();

final chat = client.private('chat.42');

chat.listen('.chat.message', (event, data) {
  print('New message: $data');
});

chat.whisper('typing', {'is_typing': true});

await chat.unsubscribe();
client.leave('chat.42');

Basic methods

Method Description
connect() Connect to the server
disconnect() Disconnect
updateAuthToken(token) Refresh token
channel(name) Public channel
private(name) Private channel
presence(name) Presence channel
leave(name) Leave channel
stopListening(event) Remove event handler
unsubscribe() Unsubscribe completely

Example

class EchoInstance extends ChangeNotifier {
  EchoInstance(this.tokenStorage);

  final TokenStorage tokenStorage;
  LaravelEchoSocketClient? _client;
  String? _userId;

  LaravelEchoSocketClient? get client => _client;

  void init() {
    final token = tokenStorage.value!;
    if (_userId != null && token.userId != _userId) _disconnect();
    if (_client != null) return;

    _userId = token.userId;

    _client = LaravelEchoSocketClient(
      SocketConfig(
        host: Uri.parse(ApiConfig.BASE_ECHO_URL),
        token: token.accessToken,
        authHeadersProvider: () => {'Authorization': 'Bearer ${token.accessToken}'},
      ),
    )..connect();

    notifyListeners();
  }

  void updateTokenIfNeeded() {
    final t = tokenStorage.value!;
    _client?.updateAuthToken(t.accessToken);
  }

  void disconnect() => _disconnect();

  void _disconnect() {
    _client?.disconnect();
    _client = null;
    _userId = null;
  }
}
Description
No description provided
Readme 79 KiB
Languages
Dart 94.5%
Swift 4.4%
Kotlin 0.9%
Objective-C 0.2%