Socket io from laravel to flutter
For those who are trying the same thing as me which wanted to connect to socket io from flutter to laravel.
This package can help you.
Dave Cruise
Updated on December 10, 2022Comments
-
Dave Cruise over 1 year
I am trying to do real time app with socket io.
I am able to connect my socket io in my laravel server and the laravel frontend by follow this guide.
I have connect with the event with code below from my frontend.
<script> window.Echo.channel('test-event') .listen('ExampleEvent', (e) => { console.log(e); }); </script>
As the code above from frontend can tell I have a channel call test-event and listen to ExampleEvent from laravel.
I wanted to have a listener to this event in my flutter app. I have chose this package in order to try to connect my laravel socket but it seem like it does not work as good as I think.
Below is the code from my flutter app so far.
SocketFlutterPlugin myIO; @override void initState() { super.initState(); initPlatformState(); } Future<void> initPlatformState() async { try { myIO = new SocketFlutterPlugin(); myIO.socket("http://localhost:6001"); myIO.connect(); // String jsonData = // '{"content":"test"}'; // myIO.emit("test-event",jsonData); myIO.on("test-event",(data){ print("its here"); }); } on PlatformException { _platformVersion = 'Failed to get platform version.'; } }
There are two problems that I have encountered and need some advice from you guys.
First: I do not know if I have connected to my laravel socket successfully.
Second: As I mentioned for laravel frontend I have one channel and one listener, but this package in flutter seem like only have channel to connect to.
Any advice?