Step by Step Creating a Basic WebSockets Application

In order to make yourself familiar with WebSockets in uniGUI the best way is to test this feature in a simple application.

1

Create a new uniGUI standalone application

Create a new uniGUI standalone application in your Delphi environment.

2

Enable WebSocketConnection on the MainModule

Open the MainModule and enable the WebSocketConnection property. Enabling this property will force sessions to establish a WebSocket connection to the server. This connection will be used to receive messages from the server.

Enable WebSocketConnection
3

Add a button to MainForm

Open MainForm and place a button on it.

Button on MainForm
4

Broadcast a message from the server

Add an event handler to the button and call BroadcastMessage as shown below.

MainForm - Button Click
procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  BroadcastMessage('msg', ['text', 'This is the message!']);
end;
5

Handle broadcasted messages on the client/session

Add an event handler to the MainForm named OnBroadcastMessage to receive and handle the broadcasted message:

MainForm - OnBroadcastMessage
procedure TMainForm.UniFormBroadcastMessage(const Sender: TComponent;
  const Msg: string; const Params: TUniStrings);
begin
  if Msg = 'msg' then
  begin
    ShowMessage(Params['text'].AsString);
  end;
end;
6

Run and test the application

Run the application. In the web browser open a new tab and navigate to the application URL (for example, http://localhost:8077 depending on your port configuration). In the web application press the button — a message window should display the text parameter.

Message displayed

You can open several browser tabs (sessions). All connected sessions should display the message instantly when the button is pressed.

Multiple sessions