Archive for January 29, 2015

Messages in Smart Mobile Studio

January 29, 2015 Leave a comment

If you have missed message coding from Delphi under Smart Pascal then you are in for a treat. The QTX library now supports application-wide messages in the browser; allowing you to send and subscribe to as many messages as you like. More or less duplicating the stuff you are used to under WinAPI.

In fact, you can subscribe to the same message in the same object as many times as you want — and also instantiate as many subscription objects as you like.

Receiving messages

Here is how you would create and setup a message-subscription for a form:

procedure TForm1.InitializeObject;
  {$I 'Form1:impl'}

  //create our message subscriber object

  // subscribe to message-type $100
  FMsgPort.Subscribe($100,procedure (Message:TQTXMessageData)
      writeln("Data received:" +;

  // subscribe to message-type $200
  FMsgPort.Subscribe($200,procedure (Message:TQTXMessageData)
      writeln("Data received:" +;

To send real objects through messages, simply use JSON to serialize it, then de-serialize it in your message handler.

Sending messages

The simplest way to send messages are through the two API methods:

  • QTX_SendMessage(Message:TQTXMessageData);
  • QTX_BroadcastMessage(Message:TQTXMessageData);

Alternatively, although highly unpractical, you could create your own message-port. The whole message system uses just a single message-port so you would never really create these yourself. If all you want to do is send and receive messages – you have more or less the same support as Delphi as for WinAPI messages — but simpler and easier to use!

Practical uses

Messages have been a part of Windows since the beginning, and it’s also hard-coded into Delphi. Their uses are almost limitless and you can use then to notify changes across controls and forms, transport data from one place to another — it’s really only creativity that sets the bounds.

What is cool about Smart Mobile Studio – is that you now have the same system under JavaScript. It works in every browser and allows your HTML5 forms and controls to communicate indirectly.

The technology is based on the HTML5 message system, which is a part of modern browsers. It’s extremely efficient and very reliable.