In the first article of the KitLab series, we have started with a very basic application using the Application kit.
However this kit is more than that : let’s take the next step, sending and receiving messages in Haiku.
In Haiku, all inter-component communication uses the BMessage system.
This allows BWindow, BView, and other BHandler objects to send, forward, and receive messages asynchronously.
This article shows how to:
Create a GUI with a BTextControl and buttons
Send messages with BMessage
Receive messages and display them in a BView
Visualize the contents of a BMessage
BMessage is the core messaging object in Haiku:
It carries a what code (a 4-character identifier)
It can hold arbitrary data using key/value pairs
It can be sent using BMessenger
BMessage enables the communication between GUI elements (like buttons) and application logic.
In Haiku, it’s important to distinguish between BLooper and BHandler:
BLooper is a message thread that receives and distributes BMessages. Examples include:
BApplication → the whole application
BWindow → a window
A “pure” BLooper → a message-handling thread without any GUI
BHandler is any object that can receive messages via MessageReceived(). Examples include:
BWindow (inherits from BLooper → so it’s both a handler and a looper)
BView → a drawing area or UI widget inside a window
BButton, BTextControl, etc. → GUI controls
Key rule: Handlers never have their own thread—they always rely on a looper.
To sum-up : Looper = distributor of messages, Handler = reactor to messages.
Here's a simple mental map:
BApplication (BLooper)
│
├─ BWindow (BLooper + BHandler)
│ ├─ BView (BHandler)
│ ├─ BButton (BHandler)
│ └─ BTextControl (BHandler)
└─ … other windows / loopers
Remember: in Haiku, handlers (BView, BButton, BWindow, etc.) rely on a looper (like BWindow or BApplication) to receive messages. A handler cannot receive messages by itself—it always needs a looper to distribute messages.
A BMessenger is a helper class that lets you safely send a BMessage to a handler, making sure it is delivered through the correct looper thread.
Think of it as a “remote control” for a handler:
BMessage = the content or data you want to send
BMessenger = delivers that message safely to the handler via its looper
The constructor signature is:
BMessenger(const BHandler* handler, const BLooper* looper = NULL, status_t* result = NULL);
handler → the target BHandler (BView, BWindow, etc.)
looper → the BLooper that owns the handler (usually the window)
result → optional status code
Using BMessenger ensures messages are always delivered correctly, even if the handler is in a different thread or not directly accessible.
In our example, the window uses a BMessenger to send an update message to the MessageView, guaranteeing that the message reaches the view safely and asynchronously.
The following program builds a simple messaging interface:
A BTextControl to enter text
Two buttons: Send and Reply
A custom MessageView to display messages
#include <Application.h>
#include <Window.h>
#include <View.h>
#include <Button.h>
#include <TextControl.h>
#include <Messenger.h>
#include <Message.h>
#include <String.h>
#include <Font.h>
#include <math.h> // for ceil()
// View that displays messages with proper line breaks
class MessageView : public BView {
public:
MessageView(BRect frame)
: BView(frame, "message_view", B_FOLLOW_ALL, B_WILL_DRAW)
{
SetViewColor(255, 255, 255); // White background
}
void Draw(BRect) override {
SetHighColor(0, 0, 0); // Black text
BFont font;
GetFont(&font);
font_height fh;
font.GetHeight(&fh);
float lineHeight = ceil(fh.ascent + fh.descent + fh.leading);
float y = 20;
BString line;
const char* str = fText.String();
while (*str) {
if (*str == '\n') {
DrawString(line.String(), BPoint(10, y));
line.Truncate(0);
y += lineHeight;
} else {
line << *str;
}
str++;
}
if (!line.IsEmpty()) {
DrawString(line.String(), BPoint(10, y));
}
}
void Append(const BString& text) {
fText << text << "\n";
Invalidate();
}
// MessageReceived handles its own messages
void MessageReceived(BMessage* msg) override {
if (msg->what == 'updt') {
const char* recvd;
if (msg->FindString("text", &recvd) == B_OK) {
Append(recvd);
}
} else {
BView::MessageReceived(msg);
}
}
private:
BString fText;
};
// Main window + controller
class MessagingWindow : public BWindow {
public:
MessagingWindow(BRect frame)
: BWindow(frame, "BMessage Example", B_TITLED_WINDOW,
B_QUIT_ON_WINDOW_CLOSE)
{
BRect bounds = Bounds();
// Message display view
fView = new MessageView(BRect(bounds.left, bounds.top,
bounds.right, bounds.top + 150));
AddChild(fView);
// Text input
fTextControl = new BTextControl(BRect(10, 160, bounds.right - 10, 180),
"input", "Message:", "", nullptr);
AddChild(fTextControl);
// Send button
BButton* sendBtn = new BButton(BRect(10, 200, 100, 230),
"send_button", "Send",
new BMessage('send'));
sendBtn->SetTarget(this); // Window handles the click
AddChild(sendBtn);
// Reply button
BButton* replyBtn = new BButton(BRect(120, 200, 220, 230),
"reply_button", "Reply",
new BMessage('repl'));
replyBtn->SetTarget(this);
AddChild(replyBtn);
}
void MessageReceived(BMessage* msg) override {
switch (msg->what) {
case 'send':
case 'repl': {
const char* text = fTextControl->Text();
BString prefix = (msg->what == 'send') ? "Sent: " : "Reply: ";
BString full = prefix << text;
// Create a message to update the view
BMessage forwardMsg('updt');
forwardMsg.AddString("text", full);
// Send the message directly to the view
BMessenger msgr(fView);
msgr.SendMessage(&forwardMsg);
break;
}
default:
BWindow::MessageReceived(msg);
}
}
private:
MessageView* fView;
BTextControl* fTextControl;
};
// Application class
class MessagingApp : public BApplication {
public:
MessagingApp()
: BApplication("application/x-vnd.example-bmessenger")
{
BRect rect(100, 100, 400, 350);
MessagingWindow* w = new MessagingWindow(rect);
w->Show();
}
};
int main() {
MessagingApp app;
app.Run();
return 0;
}
MessageView
Subclass of BView that draws text
Uses BFont::GetHeight() to calculate line spacing
Append() adds new messages and triggers a redraw
Handles multi-line text properly by splitting on \n
MessagingWindow
Subclass of BWindow acting as the controller
Handles button clicks via MessageReceived()
Reads the current text from BTextControl dynamically
Creates a BMessage with the text and sends it to the MessageView using BMessenger
Buttons
Each button has a BMessage ('send' or 'repl')
Messages are sent by default to the window (SetTarget(this))
The window then forwards the message to the view up to the Messenger
BMessenger
A simplified overview for the message workflow is as per below:
+-------------------------------------------------------+
| BWindow (BLooper/BHandler) |
| |
| +-------------------------------+ |
| | BButton | |
| +-------------------------------+ |
| | |
| | message |
| v |
| [ BWindow::MessageReceived() ] |
| | |
+------------|------------------------------------------+
|
| forwards / dispatches message
v
+-------------------------+
| BView |
| (BHandler) |
+-------------------------+
Dynamic text: always read from BTextControl at the moment the button is clicked
Line breaks: DrawString() does not handle \n, so we draw each line manually
MVC Pattern:
Model = text in BTextControl
View = MessageView responsible for displaying messages
Controller = MessagingWindow handles button clicks and forwards messages to the view
Save the code as MessagingApp.cpp, then compile:
g++ MessagingApp.cpp -o MessagingApp -lbe
Run the application:
./MessagingApp
You will see a window with a text field and two buttons :

Type a message of your choice:

Then clicking Send will append the message to the view, each on its own line:

Do the same with the Reply button:

It's working fine, isn't it ?
Change the font using SetFont() to see different line heights
Add more buttons or input fields and forward messages similarly
Explore the contents of BMessage by adding multiple keys, e.g., integers or floats
With just a few lines of code, you’ve created a fully functional messaging interface in Haiku:
Demonstrates BMessage sending and receiving
Shows how to handle dynamic user input
Uses the MVC pattern for clean architecture
In the next KitLab article, we’ll explore how to run background tasks by customizing BLooper and BHandler.
Stay tuned!