Skip to main content

spring_boot_starter_websocket 0.5.0 copy "spring_boot_starter_websocket: ^0.5.0" to clipboard
spring_boot_starter_websocket: ^0.5.0 copied to clipboard

Declarative WebSocket clients for spring_boot_dart: an interface with @WebSocketClient, @OnMessage and @SendMessage becomes a bean; JSON via ObjectMapper, reconnection.

example/example.dart

// A declarative WebSocket client in a Spring Boot application: describe the
// socket, the generator writes its implementation and picks the starter up
// (example.spring.dart, from `dart run build_runner build`). Messages are
// JSON through the ObjectMapper; the connection opens on first use and
// closes with the application.
//
//   dart run example/example.dart

import 'dart:async';
import 'dart:io' as io;

import 'package:spring_boot_starter_websocket/spring_boot_starter_websocket.dart';

import 'example.spring.dart';

@WebSocketClient('chat', url: r'${chat.url}')
abstract interface class ChatSocket {
  /// Incoming messages, JSON → ChatMessage.
  @OnMessage()
  Stream<ChatMessage> messages();

  /// ChatMessage → JSON.
  @SendMessage()
  Future<void> send(ChatMessage message);
}

@Introspected()
class ChatMessage {
  const ChatMessage({required this.from, required this.text});
  final String from;
  final String text;
}

@Service()
class Chat {
  Chat(this.socket);
  final ChatSocket socket;

  /// Sends [text] and waits for the first message back.
  Future<ChatMessage> ask(String text) async {
    final reply = socket.messages().first;
    await socket.send(ChatMessage(from: 'me', text: text));
    return await reply;
  }
}

/// Asks the chat once the application has started and logs the answer;
/// `@Log` gives it the `log` getter.
@Component()
@Log(topic: 'example')
class ChatTour implements CommandLineRunner {
  ChatTour(this.chat);

  final Chat chat;

  @override
  Future<void> run(List<String> args) async {
    final reply = await chat.ask('hello');
    log.info('${reply.from}: ${reply.text}'); // me: hello
  }
}

@SpringBootApplication()
Future<void> main(List<String> args) async {
  // A local echo server standing in for a real chat server.
  final server = await io.HttpServer.bind(io.InternetAddress.loopbackIPv4, 0);
  server.listen((request) async {
    final socket = await io.WebSocketTransformer.upgrade(request);
    socket.listen(socket.add); // sends every message back
  });

  final context = await springApplication.runAsync(
    args,
    properties: {'chat.url': 'ws://127.0.0.1:${server.port}/ws'},
  );

  await context.closeAsync(); // closes the socket
  await server.close(force: true);
}
0
likes
0
points
616
downloads

Publisher

verified publisherfarum-azula.org

Weekly Downloads

Declarative WebSocket clients for spring_boot_dart: an interface with @WebSocketClient, @OnMessage and @SendMessage becomes a bean; JSON via ObjectMapper, reconnection.

Repository (GitHub)
View/report issues

Topics

#websocket #realtime #spring

License

unknown (license)

Dependencies

spring_boot_dart, spring_boot_starter_logging, spring_web_dart

More

Packages that depend on spring_boot_starter_websocket