Pipe #
Roughly speaking, pipe is data layer that ensures data exchange.
All methods that allow to pull data are contained in Pipe Server. It is a set of events that allows to listen to the events and deliver data.It is based on Node.js Event Emitters & Observer Pattern.
The PipeServer works due to the subscriptions. They help to exchange data. Every field can subscribes to the events.
PipeService has tree methods:
gudhub.on(event, address, fn)gudhub.emit(event, destination, address, params)gudhub.destroy(event, address, fn)
| Name | Type | Description |
|---|---|---|
| event | string |
accepts the name of the event |
| address | object |
accepts address of the field where the event located |
| fn | function |
accepts the function |
| destination | object |
event recipient |
| params | - |
parameters that are passed to the function |
Any type of data can be passed as a parameter.
On() #
This is the method that is called for getting subscription.
It accepts three arguments:
- event
- address
- function
import GudHub from '@gudhub/core';
const gudhub = new GudHub();
let address = {
app_id: newApp.app_id;
};
// the function will be executed when event happens
function myFunction(event, value){
console.log('new app name', value.app_name);
//if you do not want listen the event anymore, you can destroy it
gudhub.destroy('gh_app_info_update', address, myFunction);
};
gudhub.on('gh_app_info_update', address, myFunction);
Also, you can subscribe to several events at the same time:
import GudHub from '@GudHub/core';
const gudhub = new GudHub();
let address = {
app_id: 12345;
};
// function for passing two values of the argument
function myFunction(event, value){
switch(event) {
case 'gh_item_update':
alert( 'Updated Item Event!' );
break;
case 'gh_model_update':
alert( 'Updated Model Event!' );
break
}
//if you do not want listen the event anymore, you can destroy it
gudhub.destroy('gh_item_update gh_model_update', address, myFunction);
};
gudhub.on('gh_item_update gh_model_update', model_address, myFunction)
Emit() #
This is the method that is used by GudHub itself. It is called for getting existing subscription. Emit can work without recipient. That means it is called even if no one has subscribed.
It takes four arguments:
- event
- destination
- address
- params
In addition to existing events, you can devise your own event and emit it.
| Event name | Address | Description |
|---|---|---|
| gh_apps_list_update | - | allows to update the application list |
| gh_apps_list_refreshed | - | allows to refresh app list |
| gh_app_info_update | app_id |
updates application information |
| gh_app_views_update | app_id |
updates application views |
| gh_document_insert_one | app_id, item_id, element_id |
adds a document or updates a document if it exists |
| gh_filter_items | - | allows to filter items in the application |
| gh_items_add | app_id |
adds items to the item list |
| gh_item_update | app_id, item_id |
updates the value of any item field |
| gh_items_update | app_id |
updates the value of items field |
| gh_model_update | app_id, field_id |
updates field model in field_list |
| gh_model_delete | - | deletes field model from field_list and from items |
| gh_file_upload | app_id, item_id, file_id |
allows to add file to the file list |
| gh_file_update | app_id, file_id |
updates file data in the file list |
| gh_file_delete | app_id, file_id |
deletes file from the file list |
| gh_value_update | app_id, item_id, field_id |
updates field value in item |
The example of using the emit method:
this.pipeService.emit(
"gh_app_views_update",
{ app_id: newApp.app_id },
newApp.views_list
);
WebSocket Bridge (/ws/emit-to-user)
#
In addition to the built-in events listed above, gudhub.pipeService is also used as a bridge for real-time messages received through the /ws/emit-to-user WebSocket message.
When such a message arrives, WebsocketHandler parses its response and re-emits it through the pipe:
const emitToUserData = JSON.parse(message.response);
gudhub.pipeService.emit(emitToUserData.service_id, { user_id: message.user_id }, emitToUserData);
To receive these messages, a service must:
- send data via
gudhub.emitToUser(user_id, data)with a uniqueservice_idalways included indata(e.g."gudhub_messenger"). It is used as the pipe event name. - subscribe with
gudhub.pipeService.on(service_id, { user_id }, fn), whereuser_idis the ID of the other participant - the same value that arrives in the WebSocket message asmessage.user_id(the message sender, from the recipient's perspective).
| Property | Type | Description |
|---|---|---|
| service_id | string |
required. Identifies the service/feature the message belongs to (e.g. "gudhub_messenger"). Used as the pipe event name. |
| user_id | string or number |
ID of the other participant. Used as the pipe address ({ user_id }). |
Example
Sending a message from the server:
await gudhub.emitToUser(recipient_id, {
conversation_id: "f47ac10b-58cc-4372-a567-0e02b2c3d479",
message: { message_id: "...", user_id: sender_id, content: "Hello", type: "text", timestamp: Date.now() },
service_id: "gudhub_messenger"
});
Subscribing to it on the client:
function handleNewMessage(error, data){
console.log('new message', data.message);
};
gudhub.pipeService.on('gudhub_messenger', { user_id: otherUserId }, handleNewMessage);
// if you do not want to listen to the event anymore, you can destroy it
gudhub.pipeService.destroy('gudhub_messenger', { user_id: otherUserId }, handleNewMessage);
Destroy() #
For some time, all of subscriptions are accumulated in memory. And they do not know that they must be destroyed for optimum work. So, the destroy method exists for ruining extra subscriptions and for optimization the process.
If the destroy method is contained in a function, it means that the event is a one-time event.
Destroy takes the same arguments as the on method: event, address, function.
import GudHub from '@gudhub/core';
const gudhub = new GudHub();
let address = {
app_id: newApp.app_id;
};
// the function will be executed when event happens
function myFunction(event, value){
console.log('new app name', value.app_name);
//if you do not want listen the event anymore, you can destroy it
gudhub.destroy('gh_app_views_update', address, myFunction);
};
gudhub.on('gh_app_views_update', address, myFunction);