Ayush Oli
Ayush Oli

Reputation: 49

Unable to listen to Socket IO from Laravel Server in Flutter App

I have been using Laravel as backend to create my flutter app. I am building a live chat system and it has been working on the web version in Laravel. However, I am trying to listen to the socket messages and have tried almost every packages to listen to Laravel Socket like flutter_socket_io and tried laravel_echo also but the documentation is a little misleading. There is no option to add the server link and listen to the Event. So I am confused whether I am missing something or there isn't a good package for Laravel, Socket IO and Flutter.

Upvotes: 1

Views: 2521

Answers (1)

ajay
ajay

Reputation: 1010

There is pretty good documentation at laravel_echo plugin, make sure you follow the documentation.

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

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

  Echo echo = new Echo({
    'broadcaster': 'socket.io',
    'client': IO.io,
    'host': 'https://example.com:6001,   //url:port
    'auth': {
      'headers': {'Authorization': 'Bearer $token'}
    }
  });


  echo.join('your_channel_name').here((users) {
    print(users);
  }).joining((user) {
    print(user);
  }).leaving((user) {
    print(user);
  }).listen('PresenceEvent', (e) {
    print(e);
  });

If you are using a secure server, you can use the badCertificateCallback callback of HttpClient and just return true. This will accept all the bad certificates.

class MyHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext context) {
    return super.createHttpClient(context)
      ..badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
  }
}


void main() {
  HttpOverrides.global = new MyHttpOverrides();
  runApp(MyApp());
}

Upvotes: 2

Related Questions