Skip to content

jamiewest/long_polling_channel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

long_polling_channel

long_polling_channel offers a lightweight, cross-platform way to build bi-directional messaging over HTTP long polling. The client is backed by package:http, so it runs on the Dart VM and the web, while the server-side handler plugs straight into Shelf.

Installation

Add the package to your pubspec.yaml:

dependencies:
  long_polling_channel: ^0.0.1

Quick start

import 'package:long_polling_channel/long_polling_channel.dart';

Future<void> main() async {
  final channel = LongPollingChannel.connect(
    Uri.parse('https://example.com/long-poll'),
  );

  channel.stream.listen((message) {
    print('Received: $message');
  });

  channel.sink.add('Hello from the client');
}

Serving with Shelf

The package includes a LongPollingHandler that exposes a long polling endpoint compatible with the client channel:

import 'package:long_polling_channel/long_polling_channel.dart';
import 'package:shelf/shelf.dart' as shelf;

final handler = LongPollingHandler(Uri.parse('/long-poll'));

shelf.Handler router = (request) async {
  if (request.url.path == 'long-poll') {
    return handler.handler(request);
  }
  return shelf.Response.notFound('');
};

The handler keeps track of connected clients, buffers outbound messages until acknowledged, and exposes a stream of LongPollingConnection instances via handler.connections.

Example

Check out example/long_polling_channel_example.dart for a complete script that spins up a Shelf server, connects a client, and demonstrates request/response messaging over long polling.

Run it with:

dart run example/long_polling_channel_example.dart

License

This project is licensed under the MIT License. See LICENSE for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages