Smart Mobile Studio
  • News
  • Forums
  • Download
  • Store
  • Showcases
    • Featured demos
    • The Smart Contest 2013, Round 1 – Graphics
  • Documentation
    • Get the book
    • System requirements
    • Prerequisites
    • Getting started
      • Introduction
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • Themes and styles
    • Project types
      • Visual project
      • Game project
      • Console project
    • Layout manager
    • Networking
      • TW3HttpRequest
      • TW3JSONP
      • Loading files
  • About

Server-side Node.js (part 2): Real-time duplex communication

Posted on 08.01.2014 by AndreM Posted in News

After the simple introduction of node.js in part 1, we are going to show you how to use realtime communication with server side pushing(!) data directly to all clients. So goodbye polling, hello full duplex websockets!

Server

Create a new node.js server project (or continue from part 1) and add the following code:

var value := 0;
var io := socketio().listen(server);
io.sockets.on('connection',                     //wait for connections
   procedure(socket: JSocketIO)
   begin
       socket.emit('dataPushed', ['test']);     //push some test data to client on connection
       socket.on('requestFromClient',          //wait for a special request from the client  
       procedure(data: Variant; callback: JSocketIODataFunction)
       begin
         Console.log('Received from client: ' + data);
         value += data;
         if value > 100 then value := 0;
         callback(value);                             //send some data back
       end);
   end);

You also need to download and add ‘socket.io‘ to the uses clause.

The result should look like this:

We added here the socket.io library to our existing http server, which works on top of websockets (part of HTML5). It is event based, and by using the “on” function you can register your callback to a built-in event (like “connection”) or use your own callback name (note: this order is important: first wait for a new connection and then add your own events).

The ’emit’ function sends a named event with some data to the client, and when we receive an event from the client we can respond with some data using the supplied callback (note: when the client hasn’t supplied a callback, the callback param will be nil!).

npm

Before we can run this server, we need to install the socket.io library in node.js first: open a command promt and type:

npm install socket.io

Client

Start a new instance of the Smart Mobile Studio IDE and create a new “Visual components” project. Add 2 buttons and 2 progressbars to the form like this:

Be sure to set max or ‘Total’ value to 100.
Double click on the buttons to add an onClick event like this:

procedure TForm1.W3Button1Click(Sender: TObject);
begin
 FSocket.emit('requestFromClient', [10],
   procedure(aData: variant)
   begin
     pgProgress1.Value := aData;
   end);
end;

procedure TForm1.W3Button2Click(Sender: TObject);
begin
 FSocket.emit('requestFromClient', [5],
   procedure(aData: variant)
   begin
     pgProgress2.Value := aData;
   end);
end;

Download and add “socketioclient” to the interface uses clause (and copy the file socketio.js in the project folder) and add a “FSocket” field to TForm1:

uses
  W3System, ...
  SocketIOClient;

type
 TForm1 = class(TW3form)
   procedure W3Button2Click(Sender: TObject);
   procedure W3Button1Click(Sender: TObject);
 private
   FSocket: JSocketIO;
…

And create a new connection at startup:

procedure TForm1.InitializeObject;
begin
 inherited;
 {$I 'Form1:impl'}
 FSocket := socketio.connect('http://localhost:80');

Run
We are now ready to run both projects! First start the server and then the client.
The server console will show something like this:

The client looks like this, after pressing the button (note: I used the Android-Holo theme in the “Linker” tab of the project “Options”):

And the server window shows all kind of debug information about this too:

The funny thing is: this is realtime! We could also register an event callback at the client side, and broadcast the new value to all clients (so you would see the new value in all clients in realtime).

You can try this on your own, by adding the following line in the server:

io.sockets.emit('dataFromServer', [value]);  //send to all clients

And the following in the client:

 FSocket := socketio.connect('http://localhost:80');
 FSocket.on('dataFromServer',
   procedure(aData: variant)
   begin
     pgProgress1.Value := aData;
     pgProgress2.Value := aData;
   end);

Start the server and a couple of clients, press a button and watch all clients being updated in realtime!

Code

You can download and run the demo from here:

  • Server
  • Client
« Server-side Node.js (part 1): Introduction
Server-side Node.js (part 3): Node.js in the cloud (free hosting!) »

Pages

  • About
  • Feature Matrix
  • Forums
  • News
  • Release History
  • Download
  • Showcases
    • The Smart Contest 2013, Round 1 – Graphics
  • Store
  • Documentation
    • Creating your own controls
    • Debugging, exceptions and error handling
    • Differences between Delphi and Smart
    • Get the book
    • Getting started
      • Introduction
      • Local storage, session storage and global storage
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • pmSmart Box Model
      • Themes and styles
    • Layout manager
    • Networking
      • Loading files
      • TW3HttpRequest
      • TW3JSONP
    • Prerequisites
    • Real data, talking to sqLite
    • System requirements
    • Project types
      • Visual project
      • Game project
      • Console project

Archives

  • December 2019
  • December 2018
  • November 2018
  • July 2018
  • June 2018
  • February 2018
  • September 2017
  • April 2017
  • November 2016
  • October 2016
  • September 2016
  • April 2016
  • March 2016
  • January 2016
  • October 2015
  • September 2015
  • July 2015
  • April 2015
  • January 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011

Categories

  • Announcements (25)
  • Developers log (119)
  • Documentation (26)
  • News (104)
  • News and articles (16)

WordPress

  • Register
  • Log in
  • WordPress

Subscribe

  • Entries (RSS)
  • Comments (RSS)
© Optimale Systemer AS