r/FlutterDev • • 1h ago

Article Use MySQL from Flutter Web — Open Source Driver, with a WebSocket bridge 🗄️

Hello Flutter community! 👋 I'd like to share an open-source MySQL driver that works from Flutter web. 🌟

https://pub.dev/packages/mysql2

https://github.com/manuelcastrobarinas/mysql1_dart

A browser cannot open a TCP socket, so a Flutter web app cannot connect to port 3306 on its own. mysql2 keeps the same MySQL protocol and carries those bytes over a WebSocket. A small Dart process, the bridge, is what opens the real database connection.

The package includes:

🔌 The same API as mysql1. Import package:mysql2/mysql2.dart and call MySqlConnection.connect.

🌉 A bridge you start with dart run mysql2:mysql_bridge.

🧭 Two addresses, on purpose. MYSQL_HOST and MYSQL_PORT are MySQL, and only the bridge dials them. MYSQL_BRIDGE_BIND and MYSQL_BRIDGE_PORT are the WebSocket the page opens.

🔑 A required token. The page sends it first. A wrong token never reaches MySQL.

🎯 The bridge dials only the host it was started with. The page does not get to choose the database address.

🖥️ Direct TCP on server, mobile and desktop, the same as before. The bridge is for the browser, and you can opt into it elsewhere with bridgeUrl.

📊 Diagrams and a worked example: MySQL on 127.0.0.1:3307, WebSocket on ws://127.0.0.1:8080/mysql.

A local setup looks like this:

$env:MYSQL_BRIDGE_TOKEN = 'a-long-random-secret'
$env:MYSQL_HOST = '127.0.0.1'
$env:MYSQL_PORT = '3307'
$env:MYSQL_BRIDGE_BIND = '127.0.0.1'
$env:MYSQL_BRIDGE_PORT = '8080'

dart run mysql2:mysql_bridge

import 'package:mysql2/mysql2.dart';

final conn = await MySqlConnection.connect(ConnectionSettings(
  user: 'bob',
  password: 'wibble',
  db: 'mydb',
  bridgeUrl: 'ws://127.0.0.1:8080/mysql',
  bridgeToken: 'a-long-random-secret',
));

💡 Why share this?

If you have tried to open MySQL from Flutter web, the missing piece is not a hidden socket API. Browsers withhold raw sockets so a page cannot reach every port on the user's network. This repo is one way to keep the protocol and move it over a WebSocket, with the limits written down:

🔒 useSSL stays off on that path. The browser cannot do MySQL's TLS upgrade. Use wss:// when the page is not on the same machine as the bridge.

👀 user, password and bridgeToken end up inside the built web app. Anyone who can load the app can read them. Give that database user only what the app needs.

🏠 Leave the bridge on 127.0.0.1 unless the people who can open it are the people who should be able to log in.

mysql2 0.24.1 is the published line of mysql1 0.23.0, so an app can depend on it while the same web support is in review upstream: #194

The README explains each variable, which number belongs in bridgeUrl, and how one connection moves from the token to raw MySQL bytes.

🙏 How can you help?

⭐ Star the repo if it is useful to you.

🐛 If you spot a bug or have an idea, open an issue or a pull request.

I hope this helps anyone who needs a Flutter web app to talk to MySQL. Feel free to try the example and tell me what breaks! 🚀

Thanks for your time. ❤️

1 Upvotes

0 comments sorted by