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

1,483

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());
}
Share:
1,483
Ayush Oli
Author by

Ayush Oli

Updated on December 24, 2022

Comments

  • Ayush Oli
    Ayush Oli 11 months

    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.

    • Kamlesh Paul
      Kamlesh Paul about 3 years
      your pusher because from laravel you can fire event and pusher sdk you can use to listen that event
    • Ayush Oli
      Ayush Oli about 3 years
      No I am not using pusher but I am using socket.io. And firing the event is not the problem. Event is being fired and I can listen it on website which also has chat system. It's on the client side and listening to the event that I sent from server.
  • Ayush Oli
    Ayush Oli about 3 years
    Thank you for the reply. I got it to working before your reply. But anyway thanks a lot for the help. I was having the error because I forgot to add "Bearer" to Authorization.