Smart Mobile Studio, datasets and websockets
If you have been playing with the beta you have probably noticed a few cool new units in the RTL. First there is system.memory, system.typecon, followed by system.streams. And last but not least: system.dataset.
So, with all these units firmly in place we can now enjoy the same level of power, simplicity and elegance as under native Delphi or freepascal. And this is where TW3Dataset comes in. Because it’s actually a fully OOP dataset which saves to a very simple binary format (!)
Well that is now a thing of the past. For simple in-memory dataset chores, TW3Dataset covers your basic needs more than adequate. It also saves to TStream, meaning that saving and loading from the new storage system (more about that later) is reduced to a “one liner”.
Is it hard to use?
Nope. In fact it’s even simpler to use than TClientDataset under Delphi (or more or less identical, except with no indexing just yet). Here is a short example of how to create a dataset, populate it and save it to a stream:
var mDataset: TW3Dataset; mData: TStream; x: Integer; begin mDataset := TW3Dataset.create; try mDataset.FieldDefs.Add('id',ftAutoInc); mDataset.FieldDefs.add('name',ftString); mDataset.CreateDataset; for x:=1 to 1000 do begin mDataset.append; mDataset.Fields.FieldByName('name').AsString := 'Name #' + x.toString; mDataset.Post; end; mData := TMemoryStream.create; try mDataset.SaveToStream(mData); writeln('Dataset bytesize = ' + mData.size.toString); finally mData.free; end; finally mDataset.free; end;
As you can see from the code above some calculated field types are supported (Autoinc), and TW3Dataset is going to see some heavy expansion as it’s merged with my universal DB framework. Think FireDac but for the browser.
Other cool stuff
So in fact, you can now create and populate a full SQLite DB and just ship it to your server “as is”. Add encryption and you have a pretty good syncronization layer to play with.
W3C, WC3 or just WC
True binary streams? You got it! Compression routines ported directly from Delphi? No problem. In-memory databases? Yup!
Websockets made human
Websocket, oh what a magical buzzword! With all the hype you should think it was black magic. Well our SmartCL.Inet unit has now been expanded with a very cool WebSocket client. This means that if you want to talk directly with a nodeJS installation, or perhaps even better — write your own websocket server and get into Amazon cloud services for next to nothing, the RTL now makes that possible.
Writing a websocket client is now reduced to:
FSocket.Connect("wss://echo.websocket.org",); FSocket.OnOpen:=Procedure (Sender:TW3WebSocket) begin writeln("Socket connected"); Sender.Write("Rock it with HTML5 WebSocket"); end; FSocket.OnClosed:=procedure (Sender:TW3WebSocket) begin Writeln("Socket disconnected"); end; FSocket.OnMessage:=Procedure (Sender:TW3WebSocket;Data:String) begin Writeln("Data received:" + Data); end; FSocket.OnError:=Procedure (Sender:TW3WebSocket) begin writeln("Socket has errors"); end;
File storage for the rest of us
To make this usable by human beings, the RTL introduces a concept called “file-actions”. They work more or less like TAction under Delphi, but are especially tailored for loading and saving data in a uniform way.
Here is an example of saving and then loading back a small file:
procedure TForm1.W3Button4Click(Sender: TObject); var mAction: TFileActivitySave; begin mAction:=TFileActivitySave.Create; mAction.FileName:="document.txt"; mAction.FileMode:=fmPermanent; mAction.OnExecute:=procedure (sender:Tobject) begin writeln("Saving done"); var mLoader:=TFileActivityLoad.Create; mLoader.FileData:=TMemoryStream.Create; mLoader.FileName:='document.txt'; mLoader.FileMode:=fmPermanent; mLoader.OnExecute:=Procedure (sender:TObject) begin writeln("Loading done!"); writeln("Bytes ready = " + mLoader.FileData.Size.tostring); try var mReader:=TReader.Create(mLoader.FileData); writeln(mReader.ReadString); except on e: exception do writeln(e.message); end; end; mLoader.Execute; end; mAction.FileData:=TMemoryStream.Create; var mTemp:=TWriter.Create(mAction.FileData); mTemp.WriteString("This is some data"); mAction.Execute; end;
As you begin to explore the additions to the RTL in the next release, you will find that a lot of time and effort has gone into this. So I hope everyone get’s excited when they what they can now do — and that object pascal is the perfect language for HTML5 and mobile application development!