Coder Social home page Coder Social logo

echo's Introduction

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

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

In your pubspec.yaml file:

dependencies:
  ...
  flutter_pusher_client: ^0.1.0
  laravel_echo:

import flutter_pusher_client

import 'package:flutter_pusher_client/flutter_pusher.dart';
var options = PusherOptions(host: '10.0.2.2', port: 6001, encrypted: false);
FlutterPusher pusher = FlutterPusher('app', options, enableLogging: true);

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

Option Description Default
auth
authEndpoint /broadcasting/auth
broadcaster socket.io
crsfToken
host Socket host http://localhost:6001
namespace Event namespace App.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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.