# negotiate method

1. @override

Future <void> negotiate(

1. XMPPStreamObject event

)
override

Called whenever the stream buffer outputs a new event `event`.

## Implementation

    @override
    Future<void> negotiate(XMPPStreamObject event) async {
      switch (_state) {
        case ComponentToServerState.idle:
          if (event is XMPPStreamHeader) {
            streamId = event.attributes['id'];
            assert(
              streamId != null,
              'The server must respond with a stream header that contains an id',
            );

            _state = ComponentToServerState.handshakeSent;
            sendNonza(
              XMLNode(
                tag: 'handshake',
                text: await _computeHandshake(streamId!),
              ),
            );
          } else {
            log.severe('Unexpected data received');
            await handleError(UnexpectedDataError());
          }
          break;
        case ComponentToServerState.handshakeSent:
          if (event is XMPPStreamElement) {
            if (event.node.tag == 'handshake' &&
                event.node.children.isEmpty &&
                event.node.attributes.isEmpty) {
              log.info('Successfully authenticated as component');
              await onNegotiationsDone();
            } else {
              log.warning('Handshake failed');
              await handleError(InvalidHandshakeCredentialsError());
            }
          } else {
            log.severe('Unexpected data received');
            await handleError(UnexpectedDataError());
          }
          break;
      }
    }