Frequently Asked Component Specific Questions

Options

Display all FAQ items

Search FAQ items:


Displaying items 1 to 1 of 1, page 1 of 1

<< previous next >>

TMS MQTT
Sending and receiving images with TMS MQTT

The following code shows you how to send a selected image to a specific topic on the broker:

procedure TMQTTExampleForm.SetProfilePictureButtonClick(Sender: TObject);
var
  fs: TFileStream;
  payload: TBytes;
begin
  SelectProfileImageDialog.Filter := ''JPG images|*.jpg'';
  if (SelectProfileImageDialog.Execute) then
  begin
    fs := TFileStream.Create(SelectProfileImageDialog.FileName, fmOpenRead);
    try
      SetLength(payload, fs.Size);
      fs.Read(payload[0], fs.Size);
      MQTTClient.Publish(''myapp/profile/image'', payload);
    finally
      fs.Free;
    end;
  end;
end;
On the receiving end, this code snippet can be used to get the file and save it back to the file system:

procedure TMQTTExampleForm.PublishReceived(ASender: TObject; APacketID: Word; ATopic: string; APayload: TBytes);
var
  fs: TBytesStream;
begin
  if (ATopic = ''myapp/profile/image'') then
  begin
    fs := TBytesStream.Create(APayload);
    try
      fs.SaveToFile(''c:\temp\mqtt\profile-image.jpg'');
    finally
      fs.Free;
    end;
  end;
end;