Browse Source

still somewhat faulty http service

enzo 2 weeks ago
parent
commit
f96f40ac05
8 changed files with 357 additions and 312 deletions
  1. 3 3
      .env
  2. 1 1
      src/connector/adapter.manager.ts
  3. 168 0
      src/interface/interface.ts
  4. 135 174
      src/test/proxy.ts
  5. 1 1
      src/test/receiver.ts
  6. 0 98
      src/test/receiver.txt
  7. 4 4
      src/test/transmitter.ts
  8. 45 31
      src/utils/http.utils.ts

+ 3 - 3
.env

@@ -1,6 +1,6 @@
 ;Transport = "Websocket, Http"
 ;PORT = 3000, 3001
-Transport = "Websocket"
-# Transport = "Http"
+# Transport = "Websocket"
+Transport = "Http"
 PORT = 3000
-FolderPath = "E:/Task/Fis-TransportInterface/clients/"
+FolderPath = "E:/Task/Fis-MessageTransmission/clients/"

+ 1 - 1
src/connector/adapter.manager.ts

@@ -74,7 +74,7 @@ export class AdapterManager implements AdapterManagerInterface {
                 this.console.log({ message: `Just Double Checking... this is http` })
                 // Additional Http-specific setup if needed.
                 if (isClient) {
-                    transportService.startClient(`http://localhost:3000`)
+                    transportService.startClient(config.connection.transmitter)
                 } else {
                     transportService.startServer(transportSet.port)
                 }

+ 168 - 0
src/interface/interface.ts

@@ -0,0 +1,168 @@
+import { BehaviorSubject, Observable, Subject } from "rxjs"
+import { RetransmissionService } from "../utils/retransmission.service"
+import { WrappedMessage } from "../utils/message.ordering"
+
+/* EVENT BUS */
+export interface GeneralEvent {
+    id: string,
+    event: EventType,
+    date: Date
+    message?: string
+}
+
+export interface TransmissionEvent extends GeneralEvent {
+    transmission: Transmission
+}
+
+export interface AdapterEvent extends GeneralEvent {
+    adapters: AdapterBase | AdapterBase[]
+}
+
+export interface TransportEvent extends GeneralEvent {
+    data: any
+}
+
+/* MANAGEMENT */
+export interface MessageTransmissionManager {
+    subscribe(): Observable<TransmissionEvent>
+}
+
+export interface AdapterManager {
+    subscribe(): Observable<AdapterEvent>
+}
+
+
+/* TRANSMISSION COMPONENTS */
+export interface MessageTransmissionBase {
+    adapters: AdapterBase[]
+    mainAdapter: AdapterBase
+
+    getInfo(): TransmissionProfile
+    setUpAdapter(adapter: AdapterBase): void
+}
+
+export interface MessageReceiver extends MessageTransmissionBase {
+    receiverProfile: ReceiverProfile
+
+    getMessageBus(bus: Bus): Observable<any>
+    setReceiver(receiverProfile: ReceiverProfile, event: Observable<TransportEvent>): void
+}
+
+export interface MessageTransmitter extends MessageTransmissionBase {
+    transmitterProfile: TransmitterProfile
+    retransmission: RetransmissionService
+
+    setTransmitter(transmitterProfile: TransmitterProfile, event: Observable<TransportEvent>): void
+}
+
+export interface MessageRequestResponse extends MessageTransmissionBase {
+    transmitterInstance: MessageTransmitter
+    receiverInstance: MessageReceiver
+
+    setTransmissionProfile(transmissionInfo: MessageTransmitter, receiverInfo: MessageReceiver): void
+}
+
+
+export interface AdapterProfile {
+    id: string,
+    transportType: Transport,
+}
+
+
+
+
+export interface AdapterBase {
+    connector: TransportService // this one will refer to the actual tranpsort service like websocket and so on
+    adapterProfile: AdapterProfile
+    event: Subject<TransportEvent>
+
+    getInfo(): AdapterProfile
+    setAdapterProfile(id: string, transportType: Transport): void
+}
+
+
+export interface TransmitterAdapter extends AdapterBase {
+    emit(message: WrappedMessage): void
+}
+
+export interface ReceiverAdapter extends AdapterBase {
+    getMessageBus(bus: Bus): Observable<any>
+}
+
+/* Utils */
+export interface TransmissionProfile {
+    id: string,
+    name: string,
+    dateCreated: Date
+}
+
+export interface TransmitterProfile extends TransmissionProfile {
+}
+
+export interface ReceiverProfile extends TransmissionProfile {
+}
+
+export interface RequestResponseProfile extends TransmissionProfile {
+
+}
+
+export enum Bus {
+    GeneralBus,
+    ResponseMessageBus,
+    ErrorMessageBus,
+    NotificationMessageBus
+}
+
+
+export interface TransportMessage {
+    id: string,
+    dateCreated: Date,
+    transport: Transport,
+    target?: string,
+    payload: any
+}
+
+export interface FisMessage {
+    header: {
+        messageID: string,
+        messageName: `NotificationMessage` | `ResponseMessage` | `RequestMessage`
+    },
+    data: any
+}
+
+
+export interface Transmission {
+    clientId: string,
+    transmitter: MessageTransmitter,
+    receiver: MessageReceiver,
+    requestResponse: MessageRequestResponse,
+    event: Observable<TransportEvent>
+}
+
+export type Transport = 'Websocket' | 'Http' | 'TCP'
+
+export enum AdapterTransmissionRole {
+    Transmitter,
+    Receiver,
+    RequestResponse
+}
+
+
+
+export type EventType = 'Server Started' | 'New Client' | 'Client Connected' | 'Client Re-connected' | 'Client Disconnected' | `Server Disconnected` | 'New Message' | `Notification` | `New Server` | `Server Connected` | `New Transport` | 'New Adapter' | 'Re-Flush'
+export type ConnectionState = 'ONLINE' | 'OFFLINE'
+export interface TransportService {
+    getInfo(): Transport
+    emit(message: TransportMessage): void
+    subscribe(): Observable<TransportEvent> //all messages and whatever event will go through this, easier to implemnet across different transport protocol
+}
+
+export interface Info {
+    transport: Transport
+}
+
+export interface ClientObject {
+    id: string,
+    dateCreated: Date,
+    connectionState: BehaviorSubject<ConnectionState>
+}

+ 135 - 174
src/test/proxy.ts

@@ -1,16 +1,20 @@
 import { Socket as ClientSocket, io } from 'socket.io-client'
 import { Server, Socket as SocketForConnectedClient } from "socket.io"
-import { Observable, Subject } from "rxjs";
+import { Observable, Observer, Subject } from "rxjs";
 import { createServer, request as httpRequest } from "http";
-import http, { IncomingMessage, ServerResponse } from 'http';
-
+import express, { Response } from 'express';
+import { Express } from 'express';
+import { postAxiosRequest } from '../utils/http.utils';
+import axios from 'axios';
 let fromServer = new Subject<{ event: 'profile' | 'message', payload: any }>()
 let toServer = new Subject<{ event: 'profile' | 'message', payload: any }>()
 
-startSocketServer(3001)
+// startSocketServer(3001)
 // startSocketServer(3002)
-startClientSocketConnection('http://localhost:3000')
-// startHttpServer(3001, 'http://localhost:3000/response')
+// startClientSocketConnection('http://localhost:3000')
+startHttpServer(3001).then((app: Express) => {
+    operateHttpServer(app, 'http://localhost:3000/')
+})
 consoleLog()
 
 function consoleLog(): void {
@@ -55,6 +59,7 @@ function startSocketServer(port: number): void {
 
 }
 
+
 function startClientSocketConnection(serverUrl: string): void {
     // let clientSocket = io(serverUrl)
     let clientSocket: ClientSocket = io(serverUrl, {
@@ -84,187 +89,143 @@ function startClientSocketConnection(serverUrl: string): void {
     })
 }
 
+async function startHttpServer(port: number): Promise<Express> {
+    return new Promise((resolve, reject) => {
+        let app: Express = express();
+        // Middleware to parse JSON requests
+        app.use(express.json());
+        app.listen(port, () => {
+            console.log({ message: `Server running at http://localhost:${port}` });
+        });
+        resolve(app)
+    })
+}
 
-function startHttpServer(port: number, clientUrl: string): void {
-    // Create an observable from the client connection
-    const clientObservable = startHttpClientConnection(clientUrl);
-
-    const server = http.createServer(async (req: IncomingMessage, res: ServerResponse) => {
-        const { method, url } = req;
-
-        if (method === 'POST' && url === '/profile') {
-            // Forward the /profile request to another server (e.g., localhost:3000)
-            let body = '';
-            req.on('data', chunk => (body += chunk));
-            req.on('end', () => {
-                const options = {
-                    hostname: 'localhost',
-                    port: 3000,
-                    path: '/profile',
-                    method: 'POST',
-                    headers: {
-                        'Content-Type': 'application/json',
-                        'Content-Length': Buffer.byteLength(body),
-                    },
-                };
-
-                const forwardReq = http.request(options, forwardRes => {
-                    let forwardBody = '';
-                    forwardRes.on('data', chunk => (forwardBody += chunk));
-                    forwardRes.on('end', () => {
-                        res.writeHead(forwardRes.statusCode || 200, { 'Content-Type': 'application/json' });
-                        res.end(forwardBody); // Send back the forwarded response
-                    });
-                });
-
-                forwardReq.on('error', error => {
-                    console.error('Error forwarding /profile request:', error);
-                    res.writeHead(500, { 'Content-Type': 'application/json' });
-                    res.end(JSON.stringify({ error: 'Failed to forward request' }));
-                });
-
-                forwardReq.write(body); // Send the original request body
-                forwardReq.end();
-            });
-        } else if (method === 'POST' && url === '/response') {
-            // Handle long-polling using the observable
-            res.writeHead(200, { 'Content-Type': 'application/json' });
-
-            const subscription = clientObservable.subscribe({
-                next: data => {
-                    res.write(JSON.stringify(data)); // Send data to the client
-                    res.end(); // Close the response for this poll
-                    subscription.unsubscribe(); // Unsubscribe after sending one response
-                },
-                error: error => {
-                    console.error('Error in observable:', error);
-                    res.write(JSON.stringify({ error: 'Error occurred' }));
-                    res.end();
-                },
-            });
-
-            // Cleanup subscription if client disconnects
-            req.on('close', () => {
-                subscription.unsubscribe();
-            });
-        } else {
-            // Default 404 handler
-            res.writeHead(404, { 'Content-Type': 'application/json' });
-            res.end(JSON.stringify({ error: 'Not Found' }));
-        }
-    });
+function operateHttpServer(app: Express, url: string): void {
+    app.post('/profile', (req, res) => {
+        postAxiosRequest(url + `profile`, req.body).then((response) => {
+            res.json(response)
+        }).catch((error) => {
+            console.log(error)
+            res.json(error)
+        })
+    })
 
-    server.listen(port, () => {
-        console.log(`Server is running on http://localhost:${port}`);
-    });
-};
-
-
-/**
- * Starts a long-polling HTTP connection to the given URL and channels responses into an Observable.
- * @param url - The URL to send the long-polling requests to.
- * @returns Observable<any> - An observable emitting the response data for each poll.
- */
-function startHttpClientConnection(url: string): Observable<any> {
-    return new Observable(subscriber => {
-        const poll = () => {
-            const options = new URL(url);
-
-            const req = http.request(
-                {
-                    hostname: options.hostname,
-                    port: options.port,
-                    path: options.pathname + options.search,
-                    method: 'GET',
-                },
-                res => {
-                    let body = '';
-
-                    // Accumulate data chunks
-                    res.on('data', chunk => {
-                        body += chunk;
-                    });
-
-                    // On response end, emit the data and start the next poll
-                    res.on('end', () => {
-                        try {
-                            const parsedData = JSON.parse(body);
-                            subscriber.next(parsedData);
-                            poll(); // Start the next poll
-                        } catch (error: any) {
-                            subscriber.error('Error parsing response: ' + error.message);
-                        }
-                    });
-                }
-            );
+    app.post('/message', (req, res) => {
+        postAxiosRequest(url + `message`, req.body).then((response) => {
+            console.log(response)
+            res.json(response)
+        }).catch((error) => {
+            console.log(error)
+            res.json(error)
+        })
+    })
 
-            // Handle request errors
-            req.on('error', error => {
-                subscriber.error('Request failed: ' + error.message);
-            });
+    app.get('/poll', (req, res) => {
+        console.log('Client connected for long polling.');
+        // Flag to track if the response has been sent
+        let responseSent = false;
+        // Subscribe to the data stream
+        const subscription = handleClientHttpConnection(url).subscribe({
+            next: (message: any) => {
+                if (!responseSent) {
+                    console.log(`Sending data to client: ${JSON.stringify(message)}`);
+                    res.json(message); // Send the data to the client
+                    responseSent = true; // Mark response as sent
+                    subscription.unsubscribe(); // Unsubscribe to close this request
+                }
+            },
+            error: (err: any) => {
+                if (!responseSent) {
+                    console.error('Error in data stream:');
+                    res.status(500).send('Internal Server Error');
+                    responseSent = true; // Mark response as sent
+                }
+                subscription.unsubscribe(); // Ensure cleanup
+            },
+            complete: () => {
+                if (!responseSent) {
+                    console.log('Data stream completed.');
+                    res.status(204).send(); // No Content
+                    responseSent = true; // Mark response as sent
+                }
+                subscription.unsubscribe(); // Ensure cleanup
+            },
+        });
 
-            req.end(); // Send the request
-        };
+        // Timeout if no data is emitted within a specified duration
+        const timeout = setTimeout(() => {
+            if (!responseSent) {
+                console.log({ message: 'No data emitted. Sending timeout response.' });
+                res.status(204).send(); // No Content
+                responseSent = true; // Mark response as sent
+                subscription.unsubscribe(); // Ensure cleanup
+            }
+        }, 15000); // 15 seconds timeout (adjust as needed)
 
-        poll(); // Start the polling loop
+        // Handle client disconnection
+        res.on('close', () => {
+            if (!responseSent) {
+                console.error(`Http Client disconnected`);
 
-        // Cleanup logic: teardown observable if unsubscribed
-        return () => {
-            console.log('Stopping long-polling connection.');
-        };
+                subscription.unsubscribe(); // Ensure cleanup
+            }
+            clearTimeout(timeout); // Clear timeout to avoid unnecessary execution
+        });
     });
 }
 
 
+// For client usage
+export function handleClientHttpConnection(url: string): Observable<any> {
+    return new Observable((eventNotification: Observer<any>) => {
+        let active: boolean = true; // Flag to control polling lifecycle
 
-// Utility function to parse JSON body
-const parseBody = (req: IncomingMessage): Promise<any> => {
-    return new Promise((resolve, reject) => {
-        let body = '';
-        req.on('data', chunk => {
-            body += chunk;
-        });
-        req.on('end', () => {
-            try {
-                resolve(JSON.parse(body));
-            } catch (error) {
-                reject(error);
+        const longPoll = async () => {
+            while (active) {
+                try {
+                    // Axios request with timeout
+                    const response = await axios.get(`${url}poll`); // removing the timeout temporarily. 
+                    if (response.status === 200) {
+                        const data = response.data;
+                        eventNotification.next(data)
+                    } else if (response.status === 204) {
+                        console.log('No new messages from the server.');
+                    } else {
+                        throw new Error(`Unexpected response status: ${response.status}`);
+                    }
+                } catch (error: unknown) {
+                    console.error(`Unknown Error.`) // culprit is here
+                    // Error handling with server disconnect notification
+                    let errorMessage: string;
+
+                    if (axios.isAxiosError(error)) {
+                        if (error.response) {
+                            errorMessage = `Server returned status ${error.response.status}: ${error.response.statusText}`;
+                        } else if (error.code === 'ECONNABORTED') {
+                            errorMessage = 'Request timed out.';
+                        } else {
+                            errorMessage = error.message || 'An Axios error occurred.';
+                        }
+                    } else if (error instanceof Error) {
+                        errorMessage = error.message;
+                    } else {
+                        errorMessage = 'An unknown error occurred during polling.';
+                    }
+
+                    console.error(`Polling error: ${errorMessage}`);
+                    // observer.error(new Error(errorMessage)); // Notify subscribers of the error
+                    break; // Stop polling on error
+                }
             }
-        });
-        req.on('error', reject);
-    });
-};
-
-// Function to forward request to another server and get the response
-const forwardRequest = (data: any): Promise<any> => {
-    return new Promise((resolve, reject) => {
-        const options = {
-            hostname: 'localhost',
-            port: 3000,
-            path: '/profile',
-            method: 'POST',
-            headers: {
-                'Content-Type': 'application/json',
-                'Content-Length': Buffer.byteLength(JSON.stringify(data)),
-            },
         };
 
-        const req = httpRequest(options, res => {
-            let responseBody = '';
-            res.on('data', chunk => {
-                responseBody += chunk;
-            });
-            res.on('end', () => {
-                try {
-                    resolve(JSON.parse(responseBody));
-                } catch (error) {
-                    reject(error);
-                }
-            });
-        });
+        longPoll();
 
-        req.on('error', reject);
-        req.write(JSON.stringify(data));
-        req.end();
+        // Cleanup logic for unsubscribing
+        return () => {
+            console.log({ message: 'Unsubscribed from the long-polling channel.' });
+            eventNotification.complete(); // Notify completion
+        };
     });
-};
+}

+ 1 - 1
src/test/receiver.ts

@@ -32,7 +32,7 @@ class Supervisor {
     private handleActivity(messageTransmission: MessageTransmissionSet): void {
         // start listening to incoming messages from this client
         messageTransmission.receiver.getMessageBus(Bus.GeneralBus).subscribe((event: TransportEvent) => {
-            this.console.log({ message: `General Bus ${event.event}`, details: event })
+            this.console.log({ message: `General Bus ${event.event} ${(((event.data as TransportMessage)?.payload as WrappedMessage)?.payload as FisMessage)?.header?.messageID ?? 'Not Message'}`, details: event })
             this.generalBus.next(event)
         })
 

+ 0 - 98
src/test/receiver.txt

@@ -1,98 +0,0 @@
-/*  This is to emulate another remote process also using socket io to connect.
-TEST: to see if it performs the necessary self check to identify itself, as well as 
-receiving all the notification and response messages */
-
-// Import the necessary modules
-import { io, Socket } from "socket.io-client";
-import { handleClientSocketConnection } from "../utils/socket.utils";
-import { ConnectedServerSocket } from "../transport/websocket";
-import { v4 as uuidv4 } from 'uuid'
-import { filter, interval, map, Observable, Observer, Subject, Subscription, takeWhile } from "rxjs";
-import { TransportEvent } from "../interface/connector.interface";
-import { EventMessage, FisMessage } from "../interface/transport.interface";
-import { checkMessage, WrappedMessage } from "../utils/message.ordering";
-import { RetransmissionService } from "../utils/retransmission.service";
-
-class SocketClient {
-    private currentSocketId!: string
-    private socket!: Socket;
-    private connectedServerSocket: ConnectedServerSocket[] = []
-    private requestMessages: Subject<any> = new Subject()
-    private event: Subject<TransportEvent> = new Subject()
-    private retransmission: RetransmissionService = new RetransmissionService()
-
-    constructor(url: string) {
-        this.setUpClientServerConnection(url)
-    }
-
-    private setUpClientServerConnection(url: string) {
-        // Connect to the serve
-        this.socket = io(url);
-        // use the existing socket handler
-        handleClientSocketConnection(this.socket, this.connectedServerSocket).subscribe(this.event)
-        this.startListening(this.event)
-    }
-
-    private startListening(event: Subject<TransportEvent>): void {
-        event.subscribe((event: TransportEvent) => {
-            console.log('Event', (((event.data as EventMessage)?.payload as WrappedMessage)?.payload as FisMessage)?.header.messageID ?? 'Not Fis Message')
-            if (event.event == `New Server`) {
-                this.currentSocketId = (event.data as EventMessage).clientId
-
-                let currentClientSocket: ConnectedServerSocket | undefined = this.connectedServerSocket.find(obj => obj.id === this.currentSocketId)
-                if (currentClientSocket) {
-                    // so retransmission is working as usual
-                    this.retransmission.implementRetransmission(this.requestMessages, currentClientSocket.connectionState, true)
-                    // this.startGeneratingRequest(10000, this.requestMessages)
-                    this.retransmission.returnSubjectForBufferedItems().subscribe((message: WrappedMessage) => {
-                        this.sendMessage(message).subscribe({
-                            next: response => console.log(`Receiving response for ${message.thisMessageID}`),
-                            complete: () => console.log(`Request Completed for ${message.thisMessageID}`)
-                        })
-                    })
-                }
-            }
-        })
-    }
-
-    private startGeneratingRequest(intervalDuration: number, requestsPipe: Subject<FisMessage>) {
-        interval(intervalDuration).subscribe(time => {
-            let message: FisMessage = {
-                header: {
-                    messageID: uuidv4(),
-                    messageName: 'RequestMessage'
-                },
-                data: 'Data'
-            }
-            requestsPipe.next(message)
-        })
-    }
-
-    private sendMessage(message: WrappedMessage): Observable<WrappedMessage> {
-        return new Observable((response: Observer<WrappedMessage>) => {
-            console.log(`Emitting: ${(message.payload as FisMessage).header.messageID}`)
-            this.socket.emit('message', message)
-
-            let eventSubscription: Subscription = this.event.pipe(
-                filter(event => event.event == 'New Message'),
-                filter(event => (((event.data as EventMessage).payload as WrappedMessage).payload as FisMessage).header.messageID === (message.payload as FisMessage).header.messageID),
-                // takeWhile(event => (((event.data as EventMessage).payload as WrappedMessage).payload as FisMessage).data != 'Complete'),
-                map(event => ((event.data as EventMessage).payload as WrappedMessage)),
-            ).subscribe((message: WrappedMessage) => {
-                response.next(message)
-                if ((message.payload as FisMessage).data == 'Complete') {
-                    eventSubscription.unsubscribe()
-                    response.complete()
-                }
-            })
-        })
-    }
-}
-
-// Usage example:
-const client = new SocketClient("http://localhost:3001");
-// const client = new SocketClient("http://localhost:3002");
-// const client = new SocketClient("http://127.0.0.1:3000");
-// const client = new SocketClient("http://192.168.100.96:3000");
-
-// Send a message

+ 4 - 4
src/test/transmitter.ts

@@ -30,9 +30,9 @@ class Supervisor {
     private handleClientActivity(messageTransmission: MessageTransmissionSet): void {
         // start listening to incoming messages from this client
         messageTransmission.receiver.getMessageBus(Bus.GeneralBus).subscribe((event: TransportEvent) => {
-            this.console.log({ message: `General Bus`, details: event }) // receiving end
             let requestMessage: FisMessage = ((event.data as TransportMessage).payload as WrappedMessage).payload as FisMessage
-            // this.clientIncomingMessage.next(requestMessage)
+            this.console.log({ message: `General Bus ${requestMessage?.header?.messageID ?? 'Not a message'}`, details: event }) // receiving end
+            this.clientIncomingMessage.next(requestMessage)
             this.messageProducer.getOutgoingMessages().pipe(
                 filter(message => message.header.messageID === requestMessage.header.messageID)
             ).subscribe(message => {
@@ -55,7 +55,7 @@ class MessageProducer {
     private outgoingMessageBus: Subject<FisMessage> = new Subject()
 
     constructor(incomingMessageBus: Subject<FisMessage>) {
-        this.console.log({ message: `Constructing Message Producer`})
+        this.console.log({ message: `Constructing Message Producer` })
         this.incomingMessageBus = incomingMessageBus
 
         this.generateNotifcation().subscribe(this.generalNotification)
@@ -73,7 +73,7 @@ class MessageProducer {
     // this is called no problem
     private handleIncomingRequests(requests: Observable<FisMessage>, outgoingMessageBus: Subject<FisMessage>): void {
         requests.subscribe((request: FisMessage) => {
-            this.console.log({ message: `Generating response for new request ${request.header.messageID}`})
+            this.console.log({ message: `Generating response for new request ${request.header.messageID}` })
             this.generateMessage(request.header.messageID, 10).subscribe({
                 next: message => outgoingMessageBus.next(message),
                 error: error => this.console.log({ message: 'observer Error', details: error }),

+ 45 - 31
src/utils/http.utils.ts

@@ -9,6 +9,7 @@ import { EventMessage, FisMessage } from '../interface/transport.interface';
 import { WrappedMessage } from './message.ordering';
 import axios, { AxiosError, AxiosResponse } from 'axios';
 import ConsoleLogger from './log.utils';
+import path from 'path';
 const console: ConsoleLogger = new ConsoleLogger(`HttpUtils`, ['transport'])
 
 export function startHttpServer(port: number): Observable<ConnectedHttpClient> {
@@ -41,7 +42,7 @@ export async function initiateClientToServer(url: string, event: Subject<Transpo
                 checkOwnClientInfo(receiverProfileInfo.id).then((profile: ConnectedHttpServer) => {
                     receiverProfileInfo!.id = profile.id
                     // console.log({ message: 'jsonfile', details: profile })
-                    postAxiosRequest(url + '/profile', { name: 'Old Client', message: profile }).then((profileInfo: { name: string, message: { id: string } }) => {
+                    postAxiosRequest(url + 'profile', { name: 'Old Client', message: profile }).then((profileInfo: { name: string, message: { id: string } }) => {
                         console.log({ message: `Acknowledged as previous client. Id: ${profileInfo.message.id}` })
                         event.next({
                             id: uuidv4(),
@@ -66,7 +67,7 @@ export async function initiateClientToServer(url: string, event: Subject<Transpo
                     })
                 }).catch((error) => {
                     console.error(error)
-                    postAxiosRequest(url + '/profile', { name: 'New Client', data: null }).then((profileInfo: { name: string, message: any }) => {
+                    postAxiosRequest(url + 'profile', { name: 'New Client', data: null }).then((profileInfo: { name: string, message: any }) => {
                         updateProfileAndPublishEvent((receiverProfileInfo as ConnectedHttpServer), profileInfo, event, connectedHttpServers).then((receiverProfileInfo) => {
                             resolve(receiverProfileInfo)
                         })
@@ -76,7 +77,8 @@ export async function initiateClientToServer(url: string, event: Subject<Transpo
                     reject({ error: error, objRef: receiverProfileInfo })
                 })
             } else {
-                postAxiosRequest(url + '/profile', { name: 'New Client', data: null }).then((profileInfo: { name: string, message: any }) => {
+                console.log({ message: `Is New profile, Connecting with server` })
+                postAxiosRequest(url + 'profile', { name: 'New Client', data: null }).then((profileInfo: { name: string, message: any }) => {
                     updateProfileAndPublishEvent(receiverProfileInfo, profileInfo, event, connectedHttpServers).then((receiverProfileInfo) => {
                         resolve(receiverProfileInfo)
                     })
@@ -91,6 +93,7 @@ export async function initiateClientToServer(url: string, event: Subject<Transpo
 // For client usage
 export function handleClientHttpConnection(url: string, server: ConnectedHttpServer): Observable<TransportEvent> {
     return new Observable((eventNotification: Observer<TransportEvent>) => {
+        console.log({ message: `Long Poll Attempt for ${server.id}` })
         server.connectionState.next('ONLINE');
         let active: boolean = true; // Flag to control polling lifecycle
 
@@ -98,10 +101,10 @@ export function handleClientHttpConnection(url: string, server: ConnectedHttpSer
             while (active) {
                 try {
                     // Axios request with timeout
-                    // const response = await axios.get(`${url}/poll`); // removing the timeout temporarily. 
-                    const response = await axios.get(`${url}/poll`, {
-                        timeout: 3000, // 10s timeout this one will trigger error. That's why it keeps on throwing error
-                    });
+                    const response = await axios.get(`${url}poll`); // removing the timeout temporarily. 
+                    // const response = await axios.get(`${url}response`, {
+                    //     timeout: 3000, // 10s timeout this one will trigger error. That's why it keeps on throwing error
+                    // });
 
                     if (response.status === 200) {
                         const data = response.data;
@@ -114,7 +117,7 @@ export function handleClientHttpConnection(url: string, server: ConnectedHttpSer
                                 dateCreated: new Date(),
                                 transport: Transport.Http,
                                 target: server.id,
-                                payload: data.message,
+                                payload: data,
                             } as TransportMessage,
                         });
                     } else if (response.status === 204) {
@@ -184,7 +187,7 @@ async function updateProfileAndPublishEvent(receiverProfileInfo: ConnectedHttpSe
     return new Promise((resolve, reject) => {
         console.log({ message: `Assigned client Name: ${(profile.message as ConnectedHttpServer).id}` })
         receiverProfileInfo = profile.message as ConnectedHttpServer
-        writeFile(profile.message).then(() => {
+        writeFile(profile.message as ConnectedHttpServer, (profile.message as ConnectedHttpServer).id).then(() => {
             event.next({
                 id: uuidv4(),
                 event: `New Server`,
@@ -217,7 +220,7 @@ async function updateProfileAndPublishEvent(receiverProfileInfo: ConnectedHttpSe
     })
 }
 
-async function postAxiosRequest(url: string, data: any): Promise<any> {
+export async function postAxiosRequest(url: string, data: any): Promise<any> {
     return new Promise(async (resolve, reject) => {
         try {
             const response: AxiosResponse<any> = await axios.post(url, data);
@@ -314,16 +317,15 @@ function handleProfile(app: Express, data: { name: `Old Client` | `New Client`,
             }
         }
     }
-
-
 }
 
-export async function checkIfClientExists(id: string, filePath: string = 'clients.json'): Promise<ConnectedHttpClient> {
+export async function checkIfClientExists(id: string): Promise<ConnectedHttpClient> {
     return new Promise((resolve, reject) => {
         try {
             // Check if the file exists
+            let filePath = process.env.FolderPath as string + 'clients.json'
             if (!fs.existsSync(filePath)) {
-                console.log({ message: "File does not exist." });
+                console.log({ message: `File does not exist.` })
                 reject('File does not exist');
             }
 
@@ -331,28 +333,26 @@ export async function checkIfClientExists(id: string, filePath: string = 'client
             const fileContent = fs.readFileSync(filePath, 'utf-8');
             const data: any[] = JSON.parse(fileContent);
 
-            // Check if an object with the given id exists
+            // Check if an details with the given id exists
             let obj = data.find(entry => entry.id === id);
 
             if (obj) {
-                console.log({ message: `Client with ID ${id} exists.` });
+                console.log({ message: `Client with ID ${id} exists.` })
             } else {
-                console.log({ message: `Client with ID ${id} does not exist.` });
+                console.log({ message: `Client with ID ${id} does not exist.` })
             }
-
             resolve(obj);
         } catch (error) {
-            console.error({ message: 'Error reading the file:', details: error });
             reject(`Error reading the file`)
         }
     })
 }
 
 /* For Internal Usage only. Temporary serve as a way for server to keep track of clients. To be replaced in the future with better alternatives. */
-export function addClientToDB(entry: ConnectedHttpClient, filePath: string = 'clients.json'): void {
+export function addClientToDB(entry: ConnectedHttpClient): void {
     try {
         let data: ConnectedHttpClient[] = [];
-
+        let filePath = process.env.FolderPath as string + 'clients.json'
         // Check if the file exists and load existing data
         if (fs.existsSync(filePath)) {
             const fileContent = fs.readFileSync(filePath, 'utf-8');
@@ -415,8 +415,8 @@ export function startListeningAndStreaming(app: Express, client: ConnectedHttpCl
         const subscription = client.responseStream.asObservable().subscribe({
             next: (message: WrappedMessage) => {
                 if (!responseSent) {
-                    console.log({ message: `Sending data to client: ${JSON.stringify(message)}` });
-                    res.json({ message }); // Send the data to the client
+                    console.log({ message: `Sending data ${message.thisMessageID} to client ${client.id}}` });
+                    res.json(message); // Send the data to the client
                     responseSent = true; // Mark response as sent
                     subscription.unsubscribe(); // Unsubscribe to close this request
                 }
@@ -477,7 +477,8 @@ export function startListeningAndStreaming(app: Express, client: ConnectedHttpCl
 export async function checkOwnClientInfo(filename?: string): Promise<ConnectedHttpServer> {
     return new Promise((resolve, reject) => {
         // Check if the file exists
-        if (fs.existsSync(`${filename}.json`)) {
+        let filePath = process.env.FolderPath as string
+        if (fs.existsSync(filePath + `${filename}.json`)) {
             try {
                 // Read the file contents
                 const fileData = fs.readFileSync(`${filename}.json`, 'utf8');
@@ -503,18 +504,31 @@ export async function checkOwnClientInfo(filename?: string): Promise<ConnectedHt
 }
 
 // Specifically to write receiver profile information
-export async function writeFile(data: { id: string }): Promise<boolean> {
+async function writeFile(data: ConnectedHttpServer, filename: string): Promise<boolean> {
     return new Promise((resolve, reject) => {
+        // Ensure the folder exists
+        const folderPath = process.env.FolderPath as string
+        console.log({ message: folderPath })
+        // const folderPath = path.join(__dirname, folder);
+        if (!fs.existsSync(folderPath)) {
+            fs.mkdirSync(folderPath, { recursive: true }); // Create folder if it doesn't exist
+        } else {
+            console.log({ message: 'Folder already exist' })
+        }
+
+        // Construct the full file path (include the folder)
+        const filePath = path.join(folderPath, `${filename}.json`);
+
         // Write JSON data to a file
-        fs.writeFile(`${data.id}.json`, JSON.stringify(data, null, 2), (err) => {
+        fs.writeFile(filePath, JSON.stringify(data, null, 2), (err) => {
             if (err) {
-                console.error({ message: 'Error writing file', details: err });
-                reject(false)
+                console.log({ message: 'Error writing file', details: err });
+                reject(false);
             } else {
-                console.log({ message: 'File has been written' });
-                resolve(true)
+                console.log({ message: 'File has been written', details: filePath });
+                resolve(true);
             }
         });
-    })
+    });
 }