X-PUSH is a product that employs server push, a style of Internet-based communication where information or messages are delivered on a real-time basis. Nexacro Platform provides an XPush object, which processes messages sent from an X-PUSH server. The object allows you to request and process messages easily just with simple settings. This chapter shows you how to register an XPush object and develop an application designed to process messages, all using Nexacro Studio.
Register XPush Object
As the XPush is not a default object, you should register the object through Project Explorer of Nexacro Studio to make it available.
1
Choose the menu [File > New > Project] to create a new project.
2
Choose [TypeDefinition > Objects] in the Project Explorer to open a window that allows you to add a module.
3
Click the + button in Modules on the left side of the window. After the File Explorer is opened, select the Push.json file.
4
After finding that Push.json has been added to the module list, click the + button on the right side of nexacro.XPush to add XPush to the object list.
5
After finding that XPush has been added to the object list. click the OK button to close the window. The project will be reloaded automatically to apply the changes made by the addition of the XPush object.
Display Real-time Messages in Grid
In this section, you will learn about how to add an XPush object, receive messages from an X-PUSH server on a real-time basis and display obtained messages through a Grid.
To receive real-time messages, the X-PUSH server must be in operation with the message provider service also running. An X-PUSH server sends messages while the run_provider_demo_Push.bat file is being executed, and the transmission continues until the end of the execution of the file.
1
Choose the menu [File > New > Project] to create a Form.
2
Add a Grid and Dataset to the Form.
3
Set the columns of the Dataset as presented below through the Dataset Editor and bind the Dataset to the Grid.
The columns of the Dataset must match the IDs defined by the message layout XML file that is set by the layouturl property.
... <message type="OPDT"> <field id="id" type="string" size="9"/> <field id="content" type="string" size="255"/> </message> ...
4
Add an XPush object by dragging and dropping it from the component toolbar onto the Form design pane. Like a Dataset, the object will appear in the Invisible Object pane.
5
After selecting the added XPush object, set the layouturl and iplist properties in the Properties pane. To set layouturl, you can designate a message layout XML file by specifying the file's URL or designating its location in the local computer. To set iplist, specify the IP address and port number of the X-PUSH server.
You can select either the TCP connection (Nexacro Browser) or the HTTP connection (web browser) as a value of the iplist property.
When you select the TCP connection, you must specify 10081 as a port number.
When you select the HTTP connection, you must specify 10080 as a port number.
If you want to provide both services—NRE and WRE—simultaneously, you must specify the two port numbers together as a value of iplist.
iplist="http://localhost:10080, localhost:10081"
6
Add the onload event of the Form with the below handler, which is designed to connect to the X-PUSH server.
this.Form_Work_onload = function(obj:nexacro.Form,e:nexacro.LoadEventInfo) { this.XPush00.connect("user00", "password"); };
7
Add the onsuccess event of the XPush object with the below handler, which prompts the following actions if the connection is successfully made: the subscribe method will be called to receive messages on a real-time basis. Then, the data of the received messages will be added to the Dataset. Eventually, the messages will be displayed through the Grid.
this.XPush00_onsuccess = function(obj:nexacro.XPush,e:nexacro.XPushEventInfo) { if(e.action == nexacro.XPushAction.AUTH) // 0 { this.XPush00.subscribe("OPDT", "ALL", this, this.Dataset00, "append", "fn_PushCallback"); } }; this.fn_PushCallback = function(Row,ChangeColumn,AllColumn,Type,ActionType) { trace(Row,ChangeColumn,AllColumn,Type,ActionType); }
8
Add the onerror event of the XPush object with the below handler, which is designed to check the reason for the connect failure.
this.XPush00_onerror = function(obj:nexacro.XPush,e:nexacro.XPushErrorEventInfo) { trace(e.errormsg, e.statuscode); };
9
Upon launching the app, messages will be received after the X-PUSH server is connected within the onload event. You can see that the Grid will display messages in real time.
Reliable Messaging
The process of creating a Form and initializing an XPush object is the same in this section and in Display Real-time Messages in Grid. Therefore, this section begins its explanation with adding the onload event handler.
For reliable messaging, the X-PUSH server must be in operation with the message provider service also running. An X-PUSH server sends one message while the run_provider_demo_Reli.bat file is being executed.
Register Topic for Reliable Messaging
Reliable messaging is the concept of making certain guarantees about the successful transmission of the messages. Real-time messaging does not check whether users have received the messages after the transmission. This is like radio broadcasting, where messages keep being sent unilaterally and listeners just keep receiving them. However, reliable messaging is similar to an audio message aimed at a certain recipient. Even if users miss messages at the point of transmission, reliable messaging guarantees that they will check the messages later.
For reliable messaging, there should be a predetermined arrangement about who receives what type of messages. You can register such information in a database by calling the registerTopic method.
1
Add the onload event of the Form with the below handler, which is designed to the X-PUSH server.
this.Form_Work_onload = function(obj:nexacro.Form,e:nexacro.LoadEventInfo) { this.XPush00.connect("user00", "password"); }
2
Add the onsuccess event of the XPush object with the below handler, which prompts the following actions if the connection is successfully made: the subscribe method will be called to receive messages on a real-time basis. Then, the data of the received messages will be added to the Dataset. Eventually, the messages will be displayed through the Grid.
this.XPush00_onsuccess = function(obj:nexacro.XPush,e:nexacro.XPushEventInfo) { if(e.action == nexacro.XPushAction.AUTH) // 0 { this.XPush00.subscribe("NOTI", "ALL", this, this.Dataset00, "append", "fn_PushCallback"); } };
3
Add a Button component to the Form. Then, add the onclick event with the below handler, which is designed to add a topic under the user ID specified when the connect method was called. You should register a topic only once.
this.Button00_onclick = function(obj:nexacro.Button,e:nexacro.ClickEventInfo) { this.XPush00.registerTopic("NOTI", "ALL"); };
4
Add the onerror event of the XPush object with the below handler, which is designed to check the reason for a failure to register a topic: e.g. there has been a registered topic already.
this.XPush00_onerror = function(obj:nexacro.XPush,e:nexacro.XPushErrorEventInfo) { trace(e.errormsg, e.statuscode); };
5
Once a topic is registered, reliable messaging begins. You can check the registered topic in the T_TOPIC table of the database.
Receive Messages
If messages are sent while the app is not in operation, those messages will be saved to the T_USER_MESSAGE table of the database. if the value of the MESSAGE_STATE column is 0, it means that the messages have not been reached the user.
1
Add a Button component to the Form. Then, add the onclick event with the below handler, which is designed to check whether there are unreceived messages under the user ID specified when the connect method was called. If found, the server will send those messages, causing the value of the MESSAGE_STATE column to change to 1.
this.Button01_onclick = function(obj:nexacro.Button,e:nexacro.ClickEventInfo) { this.XPush00.requestMessage("NOTI", "ALL"); };
2
As the requestMessage method does not define a separate callback, it uses the callback defined by the subscribe method instead. Once you receive messages through the requestMessage method, you can send the response to the reception by calling the sendResponse method. After the server receives the response, the value of the MESSAGE_STATE column will turn to 2.
this.fn_PushCallback = function(Row,ChangeColumn,AllColumn,Type,ActionType,MessageId) { trace(Row,ChangeColumn,AllColumn,Type,ActionType,MessageId); if(ActionType=="RELI") this.XPush00.sendResponse(MessageId); }
Unregister Topic
If you do not need to receive messages anymore, you can call the unregisterTopic method to cancel the registered topic.
1
Add a Button component to the Form. Then, add the onclick event with the below handler, which is designed to unregister a topic under the user ID specified when the connect method was called.
this.Button02_onclick = function(obj:nexacro.Button,e:nexacro.ClickEventInfo) { this.XPush00.unregisterTopic("NOTI", "ALL"); };
2
Once you unregister the topic, the reliable messaging will also stop. You will see that the value of the ACTIVE column has been changed to "N" in the T_TOPIC table of the database, the table containing the information on registered topics.