How to make a mysql realtime database with flutter?

1,902

https://flutter.dev/docs/cookbook/networking/web-sockets use this tutorial. Also look into the concept of sockets. Keep in mind when you making a chat application you should also take into account privacy and security.

  1. Connect to a WebSocket server.

    The web_socket_channel package provides the tools you need to connect to a WebSocket server. The package provides a WebSocketChannel that allows you to both listen for messages from the server and push messages to the server.

    In Flutter, use the following line to create a WebSocketChannel that connects to a server:

    final channel = WebSocketChannel.connect(
      Uri.parse('wss://echo.websocket.org'),
    );
    
  2. Listen for messages from the server.

    Now that you've established a connection, listen to messages from the server. After sending a message to the test server, it sends the same message back. In this example, use a StreamBuilder widget to listen for new messages, and a Text widget to display them.

    StreamBuilder(
      stream: channel.stream,
      builder: (context, snapshot) {
        return Text(snapshot.hasData ? '${snapshot.data}' : '');
      },    
    )
    
  3. Send data to the server.

    To send data to the server, add() messages to the sink provided by the WebSocketChannel.

    channel.sink.add('Hello!');
    
  4. Close the WebSocket connection.

    After you're done using the WebSocket, close the connection:

    channel.sink.close();
    
Share:
1,902
omar developer
Author by

omar developer

Updated on December 22, 2022

Comments

  • omar developer
    omar developer over 1 year

    I am working on this chat app and I obviously don't have any idea how to do this but I have donr the basics such as I made the connection between my app and the mysql db and fetch the data but I think that I will need to make the db a real-time db which again I have no idea how to do so any help with that part ? also new to flutter :)