Search code examples
flutterdartwebsocketfrappe

Socket API connection between Frappe and Flutter


I am creating a mobile app using flutter as front-end and frappe as back-end (i tried in local site). In which i want to create a socket API connection for realtime updates. But i can't get any response or any errors also there is no request log in the bench log.

I have used web_socket_channel and socket_io_client packages, but nothing happens

Did anyone knows how to make it work?


Solution

  • I searched Realtime events on Frappe. It looks like Frappe using Socket IO for Realtime events. So you have to use socket_io_client package on Flutter side. I am using socket IO on my current project.

    First import package with prefix: import 'package:socket_io_client/socket_io_client.dart' as IO;

    Then define the socket with this code: IO.Socket socket = IO.io("socket_url", IO.OptionBuilder().setTransports(['websocket']).enableForceNew().build());

    Then listen events on initState with this code block

      void initState() {
    socket.onConnect((data) => log("connected"));
    socket.on("event_name", (data) {
      //handle event
    });
    super.initState();}
    

    I hope it works!