Lazarus and Miletus

Is it possible to have an example of miletus for lazarus minimal example that works.
Maybe just
MiletusShell.Execute(Command, WorkingDir: string);
or
use TMiletusINIFile

To see if my needs go in that direction

As we mentioned in your previous thread, Miletus for Lazarus is not supported currently so examples for Lazarus do not exist.

Putting that aside:

  • MiletusShell.Execute executes a command line Command from a given WorkingDir working directory.
  • TMiletusINIFile behaves similarly to TIniFile.

Many of the Miletus API (including the ones above) are promise-based. This is due to the async communication between the web application and the native layer.

If you'd like to try Miletus and see if it's something you are looking for, perhaps you can try our Visual Studio Code extension. Visual Studio Code is free and available on Linux and TMS Web Core VSC supports Miletus.

I will not make a problem for Miletus.
Neither MiletusShell.Execute nor TMiletusINIFile failed for me.
Just a small example for that, if possible

And as for VSC, I don't want to leave Pascal, I'm staying with you at Lazarus

There is no Lazarus specific example.

MiletusShell.Execute can be used like this:

//Don't forget to mark WebButton1Click as async
procedure TForm1.WebButton1Click(Sender: TObject);
var
  s: string;
begin
  s := TAwait.ExecP<string>(MiletusShell.Execute('ls'));
  WebMemo1.Lines.Add(s);
end;

Already provided a sample for TMiletusINIFile here: Lazarus, TMS WEB Core and TMiletusFirebirdDBDriver demo - #5 by Tunde

You don't need to leave Pascal. Our VSC product works with Object Pascal. See our announcement blog article.

There is no TAwait in lazarus
That's probably the problem.

I will look at VSC, thanks

An alternative syntax:

s := await(string, MiletusShell.Execute('ls'));

Thanks for trying to help, but i have a strange problem.

lazarus calls Core ..../Source/RTL/js.pas
which does not have TProc and await

.../Component Library Source/js.pas
contains TProc and await

if I add the path in Project options then I have other problems

what does 'lazarus calls' mean?

To compile a web core app, Lazarus should invoke TMSWebCompiler , that's it and nothing else.

uses WEBLib.Utils, JS;

var
s: string;
begin
s := await(string, @MiletusShell.Execute('ls'));
WebMemo1.Lines.Add(s);
end;

Panic: .../unit1.pas (114, 5) Error: await only available in async procedure

You need to mark such methods with the async attribute as explained in the doc or articles such as:

I haven't managed to get the result yet.

TForm1 = class(TWebForm)
...
procedure WebButton4Click(Sender: TObject); async;
....

procedure TForm1.WebButton4Click(Sender: TObject);
var
s: string;
begin
s := await(string, MiletusShell.Execute('ls'));
WebMemo1.Lines.Add(s);
end;

now i manage to compile

I was missing async before

i think i should get the contents of the directory
linux $: ls

It's not exactly clear what are you trying to do. Is this a regular web application from Lazarus? Then it won't work. There is no way to access something from a browser on the users' computer without any user consent or interaction, that would be a huge security risk. In that regard you can only rely on the Web APIs that are implemented by each browser.

The Miletus API only works with Miletus applications.

I made an example that works with async

TForm1 = class(TWebForm)
....
procedure WebButton4Click(Sender: TObject); async;
....

procedure TForm1.WebButton4Click(Sender: TObject);
type
TUser = record
userId: integer;
id: integer;
title: string;
body: string;
end;

TUserArray = array of TUser;

var
request: TJSXMLHttpRequest;
json: string;
javascriptarray: JSValue;
users: TUserArray;
user: TUser;

i: integer;
begin
WebMemo1.Lines.Clear;
WebHTTPRequest1.URL := 'https://jsonplaceholder.typicode.com/posts';
request := await (TJSXMLHttpRequest, WebHttpRequest1.Perform() );
json := string(request.response);
javascriptarray := TJSJSON.parse(json);
users := TUserArray(javascriptarray);

for i:= 0 to length(users) - 1 do begin
user := users[i];
WebMemo1.Lines.Add(InttoStr(user.userId)+' '+IntToStr(user.id)+' '+user.title);
WebMemo1.Lines.Add(user.body);
WebMemo1.Lines.Add('');
end;

end;
//
Miletus should be a Web API (server)
like json here
WebHTTPRequest1.URL := 'https://jsonplaceholder.typicode.com/posts';
//
When I run Miletus on linux:
$ ./Miletus
Exception Exception in module Miletus at 0000000000C75DEA.
Resource TMS.Web.Miletus.Resource.json not found.

Is there a link where I could see the work with Miletus
Some kind of instruction for Miletus

I found an interesting example here:

can i get this example for linux (libdemolib.so)
library DemoLib;

This demo is included under Demo\Miletus\Library Demo

Thanks Bruno,
I've been trying to avoid doing REST Api, looks like I'll have to do that.

This sentence pointed me in that direction:

"No need here to create a REST API interface"
TMS Software | Blog

But here I learned to use async

There might be some confusion around here, let me try to clear that up.

With Miletus you can create an application that is using a native shell application to host a web application. In other words:
You create a web application (optionally combined with the Miletus API) that gets packaged into the shell application by our compiler. The shell application contains the necessary communication logic to send data back and forth between the web and native application. This means the usage of the Miletus components and API is not necessary but at the same time you cannot use them outside of a Miletus application.

When we say there is no support for Lazarus, it means there is no Miletus project type that you could use which is necessary for the compiler that creates the Miletus application. There is no way to get it working without IDE integration.

What you tried to run on your Linux machine was probably the Miletus shell application. That shell application alone is not usable, it needs to have the web application packaged inside it.

Please consider giving the TMS Web Core VSC a try if you really want to experiment with Miletus. It has proper integration and works on Linux too.

Thank you for your reply.
I don't think I will waste your time and my time on Miletus anymore.
I watched your video instructions and saw that you work with Miletus with Raspberry pi, but TMS FNC UI Pack works fine for me on Raspberry pi.

Thank you for your time,
all the best