peekEvents method

Future<PeekEventsResponse> peekEvents({
  1. String? from,
  2. int? timeout,
  3. String? roomId,
})

This will listen for new events related to a particular room and return them to the caller. This will block until an event is received, or until the timeout is reached.

This API is the same as the normal /events endpoint, but can be called by users who have not joined the room.

Note that the normal /events endpoint has been deprecated. This API will also be deprecated at some point, but its replacement is not yet known.

from The token to stream from. This token is either from a previous request to this API or from the initial sync API.

timeout The maximum time in milliseconds to wait for an event.

roomId The room ID for which events should be returned.

Implementation

Future<PeekEventsResponse> peekEvents({
  String? from,
  int? timeout,
  String? roomId,
}) async {
  final requestUri = Uri(
    path: '_matrix/client/v3/events',
    queryParameters: {
      if (from != null) 'from': from,
      if (timeout != null) 'timeout': timeout.toString(),
      if (roomId != null) 'room_id': roomId,
    },
  );
  final request = Request('GET', baseUri!.resolveUri(requestUri));
  request.headers['authorization'] = 'Bearer ${bearerToken!}';
  final response = await httpClient.send(request);
  final responseBody = await response.stream.toBytes();
  if (response.statusCode != 200) unexpectedResponse(response, responseBody);
  final responseString = utf8.decode(responseBody);
  final json = jsonDecode(responseString);
  return PeekEventsResponse.fromJson(json as Map<String, Object?>);
}