poll

Pubbuild status

package:angel_client support for "realtime" interactions with Angel via long polling.

Angel supports WebSockets on the server and client, whichmakes it very straightforward to implement realtime collections. However, not every user's browsersupports WebSockets. In such a case, applications might gracefully degrade to long-pollingthe server for changes.

A PollingService wraps a client-side Service (typically a REST-based one), and calls itsindex method at a regular interval. After indexing, the PollingService performs a diffand identifies whether items have been created, modified, or removed. The updates are sent outthrough onCreated, onModified, etc., effectively managing a real-time collection of data.

A common use-case would be passing this service to ServiceList, a class that manages the stateof a collection managed in real-time.

  1. import 'package:angel_client/io.dart';
  2. import 'package:angel_poll/angel_poll.dart';
  3.  
  4. main() {
  5. var app = new Rest('http://localhost:3000');
  6.  
  7. var todos = new ServiceList(
  8. new PollingService(
  9. // Typically, you'll pass a REST-based service instance here.
  10. app.service('api/todos'),
  11.  
  12. // `index` called every 5 seconds
  13. const Duration(seconds: 5),
  14. ),
  15. );
  16.  
  17. todos.onChange.listen((_) {
  18. // Something happened here.
  19. // Maybe an item was created, modified, etc.
  20. });
  21. }