protected function sendReply(Response $response, Envelope $envelope)
    {
        $attributes = [
            'content_type' => 'application/json',
            'content_encoding' => 'UTF-8',
            'delivery_mode' => 2,
            'correlation_id' => $envelope->getCorrelationId(),
            'app_id' => $this->appId,
            'headers' => [
                'jsonrpc' => JsonRpcResponse::JSONRPC_VERSION,
            ],
        ];

        if ($response->isError()) {
            $payload = [
                'error' => [
                    'code' => $response->error()->code(),
                    'message' => $response->error()->message(),
                ],
                'data' => $response->data(),
            ];
        } else {
            $payload = [
                'result' => $response->result(),
            ];
        }

        $message = json_encode($payload);

        if (json_last_error() !== JSON_ERROR_NONE) {
            $message = json_encode([
                'error' => [
                    'code' => JsonRpcError::ERROR_CODE_32603,
                    'message' => 'Internal error',
                ],
            ]);
        }

        $this->exchange->publish($message, $envelope->getReplyTo(), Constants::AMQP_NOPARAM, $attributes);
    }