h-sifat / express-ipc

A simple IPC server with express-like request and route handling that also supports broadcasting to multiple channels.
MIT License
7 stars 0 forks source link

Client not node JS #2

Open jhay06 opened 6 months ago

jhay06 commented 6 months ago

Hi , I'm developing an app that runs a node JS and Qt , i used express-ipc as backend service ,how can i call express-ipc endpoint if i will use different language for client side ?

h-sifat commented 5 months ago

Hi there! Sorry for the late reply. It has been a busy week! First of all, I really appreciate that you've found my library useful and are using it in your application.

If you want to connect to expressIPC from another language, then you'll have to write a client for that language. Take a look at the client source code, don't worry, it's only 400 lines long.

It basically sends requests (serialized JSON separated by delimiter, by default, which is "\f") by writing on the socket and waiting for response.

The request has the following schema:

interface SimplifiedRequest {
  metadata: {
    id: string; // unique int string
    category: "general" | "subscribe" | "unsubscribe";
  };
  payload:
    | {
        url: string;
        query: object;
        headers: object;
        body: object | null;
        method: "get" | "post" | "delete" | "patch";
      } // general requests
    | {
        channels: string[];
      }; // for channel subscribe and unsubscribe requests
}

type SimplifiedResponse =
  | {
      // for general requests
      metadata: {
        id: string; // the int string that was sent with the request
        isError: boolean;
        category: "general" | "subscribe" | "unsubscribe";
      };
      payload: { body: any; headers: Record<string, string> };
    }
  | {
      // broadcasts
      payload: object;
      metadata: { channel: string; category: "broadcast" };
    };

So, serialized requests may look something like the following. Here, I'm using the "\n" as the delimiter so that I can put new requests in the next line. This is just for demonstration, and you should stick to the default delimiter.

{"metadata":{"id":1,"category":"general"},"payload":{"url":"/users","query":{"id":1},"method":"get"}}
{"metadata":{"id":2,"category":"subscribe"},"payload":{"channels":["exciting_news"]}}

And serialized responses.

Note: The response order is not guaranteed to be the same as the request order.

{"metadata":{"id":2,"category":"subscribe","isError":false},"payload":{"body":{"message":"Subscribed to channels: exciting_news"},"headers":{}}}
{"metadata":{"id":1,"category":"general","isError":false},"payload":{"body":{"name":"Alex","age":20},"headers":{}}}

So, go ahead and study the client's source code, and feel free to ask if you have any questions. Happy coding :smile:!

jhay06 commented 5 months ago

hi @h-sifat thank you for your response 👍

jhay06 commented 5 months ago

i tried to send he signature but did not work

client->write(QString("{\"metadata\":{\"id\":1,\"category\":\"general\"},\"payload\":{\"url\":\"/users\",\"method\":\"get\",\"query\":{\"id\":1}}\f{\"metadata\":{\"id\":2,\"category\":\"subscribe\"},\"payload\":{\"channels\":[\"exciting_news\"]}}").toUtf8());

jhay06 commented 5 months ago

okay got it working , i see that body and headers are required , its confusing on the example you gave but thank you very much :)

h-sifat commented 5 months ago

Glad you got it working :)