Pushing messages to the client
Server push
Using a technique called server push,
the server can send messages to the client. This is particularly
useful when the client needs to be notified of the completion of
an asynchronous request.
Requests are queued
The server can make both synchronous and asynchronous calls
against client-side objects. The client handles server-side requests
in the same way that the server handles client-side requests. Asynchronous
requests against a particular client-side object are queued and
processed after all synchronous requests.
Client needs to pass an object reference
To send a message to the client, the server needs to know
which client-side object to send the message to. Therefore, the
client must pass a reference to a nonvisual object to the server.
When the server receives the object reference, it automatically
creates a remote reference to the client-side object and calls one or
more functions associated with this object. Function calls made
against the remote reference are passed back over the wire to the
client that contains the object.
To ensure that messages are actually sent to the client-side
object, the client must not pass an autoinstantiated object to the
server. Instead, the client must pass a reference to an object that
is created with the CREATE statement.
What the server can do with remote references
PowerBuilder allows the server to use a remote reference just
as it would any other object reference. Once the server has a remote
reference to work with, it can place the remote reference into an
instance variable so the client does not have to send the object
reference with every request. In addition, the server can pass the
object reference to and from shared object sessions.
Example
This example illustrates the use of server push technology.
The client application makes an asynchronous call to a remote object
function. When the server has finished processing, it sends a message
back to notify the client that the request has been processed.
Client application window
The client application has a window that allows the user to
retrieve some data by issuing a call to a remote object. To initiate
the retrieval, the user presses a button that executes the following
script:
1 |
// Global variable: |
1 |
// connection myconnect |
1 |
// |
1 |
// Instance variables: |
1 |
// uo_custdata mycustdata |
1 |
// uo_response_object myresponseobject |
1 |
1 |
... |
1 |
myconnect.CreateInstance(mycustdata) |
1 |
myresponseobject = create uo_response_object |
1 |
mycustdata.<i>post</i> process_data(myresponseobject) |
1 |
... |
Note that the call to the remote object function passes a
reference to uo_response_object. This is a client-side
object that handles messages sent back by the server.
Remote object on the server
The process_data function on the uo_custdata
object takes an argument of type uo_response_object.
Here is the script for the process_data function:
1 |
// Argument: |
1 |
// uo_response_object respobject |
1 |
// |
1 |
// Perform some processing... |
1 |
... |
1 |
// Then send a message to the client... |
1 |
respobject.post doneProcessing() |
When processing has completed, the server sends an asynchronous
request back to the client. The request is added to the client’s
request queue.
Response object on the client
The doneProcessing function on the uo_response_object
notifies the user that processing has completed:
1 |
MessageBox("Processing finished", & |
1 |
"Your request has completed successfully.") |