pocketbase / dart-sdk

PocketBase Dart SDK
https://pub.dev/packages/pocketbase
MIT License
511 stars 51 forks source link

Realtime to non avail in Flutter #16

Closed TomTom101 closed 1 year ago

TomTom101 commented 1 year ago

I cannot get realtime to work neither w/ pocketbase 0.7.10 and the Dart SDK 0.4.1 nor 0.8.0-rc2 and SDK version 0.5.0-rc2.

This is my test app for pocketbase 0.7.10 and the Dart SDK 0.4.1. I created a collection items and and a user with the given credentials. The pocketbase debug log shows "Realtime connection established …" but any change in the items collections would trigger the passed function in subscribe.

import 'package:flutter/material.dart';
import 'package:pocketbase/pocketbase.dart';

void main() {
  runApp(MainApp());
}

class MainApp extends StatelessWidget {
  MainApp({super.key});

  final client = PocketBase('http://127.0.0.1:8090');

  @override
  Widget build(BuildContext context) {
    void signIn() async {
      final userData = await client.users
          .authViaEmail('existinguser@example.com', '1234567890');

      // Successfully establishes a connection, debug output is "Realtime connection established: bNuGMnn9Pmsdh9gCnkIWxnFRrL8JvYCS92ccWJLL"
      client.realtime.subscribe("items", (e) {
        // updating, inserting, deleting does not fire this function here
        print("Changed main $e");
      });
    }

    return MaterialApp(
      home: Center(
        child: ElevatedButton(
          onPressed: signIn,
          child: const Text("Realtime"),
        ),
      ),
    );
  }
}

The network shows a single GET call to /api/realtime whereas a sample React app I tested, and which works fine with realtime, additionally POSTs subscriptions etc.

Can someone post a minimal Flutter with working realtime subscriptions?

TomTom101 commented 1 year ago

Just saw this "Streamed responses (used by the realtime service) are not supported on the web". I guess this answers my questions, realtime is not supported on the web plattform. What a bugger. Anyhow, lovely project, this!

ganigeorgiev commented 1 year ago

@TomTom101 Yes, that's correct. Currently the Dart SDK realtime service doesn't work on the web platform. There are workarounds, eg. using the browser EventSource js-interop and/or use fetch to send the request, but it would be better and more maintainable in the long run if it is added in the dart http client (you can subscribe to this issue - https://github.com/dart-lang/http/issues/595).

I'll close the issue for now as it is known limitation, but if someone else think that it is worth investing the time creating a workaround, please let me know and depending on the users demand I may reconsider implementing it.

TomTom101 commented 1 year ago

Thanks Gani! I am a bit spoiled with Firebase's Firestore where I simply listen to a stream and never have to worry about updating datasets anywhere. I'd sure be interested on how to replicate a similar behavior with Pocketbase, ideally in combination with Riverpod.

agreensh commented 1 year ago

I would be interested in a workaround for this issue now (who knows how long Dart will take to be updated). Currently, I cannot use pocketbase because of this limitation on the web.