laravel_echo 0.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 90

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 same as official Echo package, so everything in Official documentation should work.

Three connectors available:

Screen Shot 2019-03-17 at 9 37 50 PM

Getting started #

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: ^0.9.1
  laravel_echo:

import socket_io_client

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

usage

// Create echo instance
Echo echo = new Echo({
  'broadcaster': 'socket.io',
  'client': IO.io,
});

// 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.socket.on('connect', (_) => print('connected'));
echo.socket.on('disconnect', (_) => print('disconnected'));

Pusher (NOT TESTED) #

DONT USE THIS FOR NOW will look at Pusher in following weeks. Any contributions welcomed.

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

In your pubspec.yaml file:

dependencies:
  ...
  pusher: ^1.0.0
  laravel_echo:
Pusher pusher = new Pusher('PUSHER_APP_ID', 'PUSHER_APP_KEY', 'PUSHER_APP_SECRET');

Echo echo = new Echo({
  'broadcaster': 'pusher',
  'client': pusher,
});

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

socket.on('connect', (_) => print('connect'));
socket.on('disconnect', (_) => print('disconnect'));

Guide #

Options #

OptionDescriptionDefault
auth
authEndpoint/broadcasting/auth
broadcastersocket.io
crsfToken
hostSocket hosthttp://localhost:6001
namespaceEvent namespaceApp.Events
...Any other options, passed as socket params

Authorize private channels #

To authorize channel requests we use Laravel Passport

In our BroadcastServiceProvider.php we need to enable

Broadcast::routes(['middleware' => ['auth:api']]);

then, when creating Echo instance include Authorization header with bearer token

echo = new Echo({
  'broadcaster': 'socket.io',
  'client': socket,
  'auth': {
    'headers': {
        'Authorization': 'Bearer $token'
    }
  }
});

Package by Kakajan SH

[0.2.1] - 17.03.2018

  • Update package description
  • Update README.md

[0.2.0] - 16.03.2018

  • Minor changes
  • Include example

[0.1.1] - 02.03.2018

  • Added Pusher connector. Implemented by @abrahamSN
  • Fixes issue with socket-io private channels
  • Cleaned up

[0.0.1] - 09.02.2018

  • Initial release.
  • Added null connector
  • Added socketio connector

example/README.md

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: ^0.9.1
  laravel_echo:

import socket_io_client

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

usage

// Create echo instance
Echo echo = new Echo({
  'broadcaster': 'socket.io',
  'client': IO.io,
});

// 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.socket.on('connect', (_) => print('connected'));
echo.socket.on('disconnect', (_) => print('disconnected'));

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  laravel_echo: ^0.2.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:laravel_echo/laravel_echo.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
80
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
90
Learn more about scoring.

We analyzed this package on Oct 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/src/connector/pusher-connector.dart. (-1.49 points)

Analysis of lib/src/connector/pusher-connector.dart reported 3 hints:

line 2 col 8: Unused import: 'package:laravel_echo/src/channel/channel.dart'.

line 3 col 8: Unused import: 'package:laravel_echo/src/channel/private-channel.dart'.

line 4 col 8: Unused import: 'package:laravel_echo/src/channel/presence-channel.dart'.

Format lib/src/channel/channel.dart.

Run flutter format to format lib/src/channel/channel.dart.

Format lib/src/channel/presence-channel.dart.

Run flutter format to format lib/src/channel/presence-channel.dart.

Fix additional 9 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/channel/private-channel.dart (Run flutter format to format lib/src/channel/private-channel.dart.)
  • lib/src/channel/pusher-presence-channel.dart (Run flutter format to format lib/src/channel/pusher-presence-channel.dart.)
  • lib/src/channel/socketio-channel.dart (Run flutter format to format lib/src/channel/socketio-channel.dart.)
  • lib/src/channel/socketio-presence-channel.dart (Run flutter format to format lib/src/channel/socketio-presence-channel.dart.)
  • lib/src/channel/socketio-private-channel.dart (Run flutter format to format lib/src/channel/socketio-private-channel.dart.)
  • lib/src/connector/connector.dart (Run flutter format to format lib/src/connector/connector.dart.)
  • lib/src/connector/null-connector.dart (Run flutter format to format lib/src/connector/null-connector.dart.)
  • lib/src/connector/socketio-connector.dart (Run flutter format to format lib/src/connector/socketio-connector.dart.)
  • lib/src/util/event-formatter.dart (Run flutter format to format lib/src/util/event-formatter.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test