Skip to content

Wiki Search RPC

This tutorial explains how to create and expose dataBridges RPC functions. dataBridges RPC provide reliable two-way messaging between multiple endpoints allowing you to build sophisticated asynchronous interactions take a look at RPC details at Detailed API documentation.

In this tutorial, we wil be exposing RPC Server wiki.search and a function called summary. The function wiki.search(summary) will give you a summary of wiki info for the search string you pass it as parameter.

For example wiki.search(summary["google"]) will return back google wiki summary.

{
  statuscode: 0,
  exectime: 1140,
  data: `Google LLC ( (listen)) is an American multinational technology company focusing on search engine technology, online advertising, cloud computing, computer software, quantum computing, e-commerce, artificial intelligence, and consumer electronics. It has been referred to as "the most powerful company in the world" and one of the world's most valuable brands due to its market dominance, data collection, and technological advantages in the area of artificial intelligence. Its parent company Alphabet is considered one of the Big Five American information technology companies, alongside Amazon, Apple, Meta, and Microsoft.\n` +
    "Google was founded on September 4, 1998, by Larry Page and Sergey Brin while they were PhD students at Stanford University in California. Together they own about 14% of its publicly listed shares and control 56% of the stockholder voting power through super-voting stock. The company went public via an initial public offering (IPO) in 2004. In 2015, Google was reorganized as a wholly owned subsidiary of Alphabet Inc. Google is Alphabet's largest subsidiary and is a holding company for Alphabet's Internet properties and interests. Sundar Pichai was appointed CEO of Google on October 24, 2015, replacing Larry Page, who became the CEO of Alphabet. On December 3, 2019, Pichai also became the CEO of Alphabet.The company has since rapidly grown to offer a multitude of products and services beyond Google Search, many of which hold dominant market positions. These products address a wide range of use cases, including email (Gmail), navigation (Waze & Maps), cloud computing (Cloud), web browsing (Chrome), video sharing (YouTube), productivity (Workspace), operating systems (Android), cloud storage (Drive), language translation (Translate), photo storage (Photos), video calling (Meet), smart home (Nest), smartphones (Pixel), wearable technology (Pixel Watch & Fitbit), music streaming (YouTube Music), video on demand (YouTube TV), artificial intelligence (Google Assistant), machine learning APIs (TensorFlow), AI chips (TPU), and more. Discontinued Google products include gaming (Stadia), Glass, Google+, Reader, Play Music, Nexus, Hangouts, and Inbox by Gmail.Google's other ventures outside of Internet services and consumer electronics include quantum computing (Sycamore), self-driving cars (Waymo, formerly the Google Self-Driving Car Project), smart cities (Sidewalk Labs), and transformer models (Google Brain).Google and YouTube are the two most visited websites worldwide followed by Facebook and Twitter. Google is also the largest search engine, mapping and navigation application, email provider, office suite, video sharing platform, photo and cloud storage provider, mobile operating system, web browser, ML framework, and AI virtual assistant provider in the world as measured by market share. On the list of most valuable brands, Google is ranked second by Forbes and fourth by Interbrand. It has received significant criticism involving issues such as privacy concerns, tax avoidance, censorship, search neutrality, antitrust and abuse of its monopoly position."
}

Project Source

Download source

Download the rpc.wiki.client by clicking on this link

Download the rpc.wiki.server by clicking on this link

Download source

Download the rpc.wiki.client by clicking on this link

Download the rpc.wiki.server by clicking on this link

Download source

Download the rpc.wiki.client by clicking on this link

Download the rpc.wiki.server by clicking on this link

Dependencies

Dependencies

For this application, you will require to use moment,wikijs,databridges-sio-client-lib (dataBridges Client Library) and databridges-sio-server-lib (dataBridges Server Library) with Node.js.

  • Install External dependencies
  • npm install moment wikijs --save

  • Install dataBridges library

  • npm install databridges-sio-client-lib databridges-sio-server-lib --save

Download the package.json by clicking on this link

Dependencies

For this application, you will require to use wikipedia,asyncio,databridges-sio-client-lib (dataBridges Client Library) and databridges-sio-server-lib (dataBridges Server Library).

  • Install External dependencies
  • pip3 install wikipedia
  • pip3 install asyncio

  • Install dataBridges library

  • pip3 install databridges_sio_client_lib
  • pip3 install databridges_sio_server_lib

Dependencies

For this application, you will require to use WikiDotNet, Databridges.Sio.Client.Lib (dataBridges Client Library) and Databridges.Sio.Server.Lib (dataBridges Server Library).

  • Install dataBridges library and WikiDotNet from "Manage NuGet Packages..." from the Solution -> Application Name context menu.

Set up a dataBridges account and app

Before we jump right into setting up an application with dataBridges, you’ll need to create a dataBridges account and app, if you don’t already have one:

  1. Sign up for a dataBridges account.

  2. Create a new app by selecting Apps and clicking Create New button.

  3. You can retrieve your app credentials from the App Keys tab.

Major Components of this example

Lets first understand how this example is structured.

This example has two files one is for running the RPC server, other is for testing the RPC server using dataBridges client library.

  • rpc.wiki.server
  • rpc.wiki.client

rpc.wiki.server serves wiki.search RPC server which has summary function exposing functionality search summary of keyword from Wikipedia using Wiki plugins depending on the language used. This function can be called by RPC client using RPC call functionlity.

const wiki = require('wikijs').default;

// Initialize RPC Server wiki.search.
let wikiServer = dbridge.rpc.init("wiki.search");

// Define exposed functions.
 // Unary RPC: the client sends a single request and receives a single response.
function summary(payload, response) {
    const startTime = moment().valueOf();
    wiki().page(payload.inparam).then(page => page.summary())
        .then((data) => {
            response.end(JSON.stringify({
                statuscode: 0,
                exectime: moment().valueOf() - startTime,
                data: data
            }));
        }).catch((err) => {
            response.end(JSON.stringify({
                statuscode: 1,
                exectime: moment().valueOf() - startTime,
                data: 'wiki api exception..' + err.source + ',' + err.code + ',' + err.message
            }));
            console.log('wiki api exception..', err.source, err.code, err.message);
        });
}
// Define all exposed functions.
wikiServer.functions = function () {
    // Bind the exposed function to summary RPC server.
    wikiServer.regfn("summary", summary);
}

wikiServer.register();
import wikipedia

# Define all exposed functions.
async def wikiServerFunctions(self):

    async def summary(inparameter, response): 
        payload = inparameter["inparam"]
        try:
            start_time = time.time()
            data = wikipedia.summary(payload)
            jsonobject = {"statuscode": 0, "exectime": (time.time() - start_time), "data": data}
            await response.end(json.dumps(jsonobject))
        except Exception as e:
            jsonobject = {"statuscode": 1,"exectime": (time.time() - start_time), "data": ""}
            await response.end(json.dumps(jsonobject))

    try:
        # Bind the exposed function, so that library can register it for receiving calls.
        self.wikiServer.regfn("summary", summary)
    except dBError as dberror:
        print("Exception: ", dberror.source, dberror.code, dberror.message)

self.wikiServer = self.dbridge.rpc.init("wiki.search")
self.wikiServer.functions = self.wikiServerFunctions

try:
    #  Register RPC Server with dBrdige network.
    await self.wikiServer.register()
except dBError as dberror:
    #  Catch any registration error.
    print("Exception: ", dberror.source, dberror.code, dberror.message)
using WikiDotNet;

// Define exposed functions.
// RPC: the client sends a single request and receives a single response.
public async void summary(object inparameter, object response) {
    dBridges.Utils.dBParams parameter = inparameter as dBridges.Utils.dBParams;
    dBridges.responseHandler.CResponseHandler responsehandler = response as dBridges.responseHandler.CResponseHandler;
    var watch = new System.Diagnostics.Stopwatch();
    string payload = parameter.inparama;

    string searchstring = payload;
    watch.Start();
    WikiSearchSettings searchSettings = new WikiSearchSettings
    { RequestId = "Request ID", ResultLimit = 5, ResultOffset = 2, Language = "en" };

    WikiSearchResponse responsewiki = WikiSearcher.Search(searchstring, searchSettings);
    string resultstring = "";
    Dictionary<string, object> resultjson = new Dictionary<string, object>();
    resultjson.Add("statuscode", 0);
    foreach (WikiSearchResult result in responsewiki.Query.SearchResults) {
         resultstring = resultstring + result.Preview;
    }
    watch.Stop();
    resultjson.Add("data", resultstring);
    resultjson.Add("exectime", watch.ElapsedMilliseconds);
    string jsonstring = JsonConvert.SerializeObject(resultjson);
    await responsehandler.end(jsonstring);
}


// Define all exposed functions.
public void wikifunction(object eventInfo) {
    try {
        Action<object, object> icurrent = this.summary;
        (eventInfo as dBridges.remoteprocedure.Crpcserver).regfn("summary", icurrent);
    } catch (Exception e) {
        Console.WriteLine(e);
    }
}

// Initialize RPC Server wiki.search.
this.wikiServer = dbridge.rpc.init("wiki.search");

Action<object> ifunctions = this.wikifunction;
this.wikiServer.functions = ifunctions;

// Register RPC Server with dBrdige network.
await this.wikiServer.register();

// Bind to server.registration.success, This will be triggered When summary is ready to serve.
Action<object, object> rconnectsuccess;
this.wikiServer.bind("dbridges:rpc.server.registration.success", rconnectsuccess = (object payload, object metadata) => {
    Console.WriteLine("dbridges:rpc.server.success {0} {1} {2} {3}", payload, metadata, this.wikiServer.getServerName(), this.wikiServer.isOnline());
});
// Bind to server.online, This will be triggered When summary is ready to serve.
Action<object, object> rconnectonline;
this.wikiServer.bind("dbridges:rpc.server.online", rconnectonline = (object payload, object metadata) => {
    Console.WriteLine("dbridges:rpc.server.online {0} {1} {2} {3}", payload, metadata, this.wikiServer.getServerName(), this.rpcwikiServerServer.isOnline());
});
// Bind to server.registration.fail, This will be triggered When any registration issue is raised.
Action<object, object> rconnectfail;
this.wikiServer.bind("dbridges:rpc.server.registration.fail", rconnectfail = (object payload, object metadata) => {
    Console.WriteLine("dbridges:rpc.server.fail {0} {1} {2} {3}", payload, metadata, this.wikiServer.getServerName(), this.wikiServer.isOnline());
});

rpc.wiki.client connects to wiki.search RPC server and executes summary function to get the result from wikipedia.

// Connect to wiki.search server
wiki = dbridge.rpc.connect("wiki.search");
// We will bind to rpc server connection success event. On success, we will call remote functions.
wiki.bind("dbridges:rpc.server.connect.success", () => {
   // Unary RPC: The client sends a single request and receives a single response.
    wiki.call("summary", searchString, 1000 * 60 * 2, null).then((response) => {
        const resp = JSON.parse(response);
        console.log(resp);
    }).catch((err) => {
        console.log(err);
    });
});
#  Connect to rpcServer
try:
    self.wiki =  await self.dbridge.rpc.connect("wiki.search")
except Exception as dberror:
    print("Exception: ", dberror.source, dberror.code, dberror.message)

if self.wiki!= None:
    async def serverconnect_success(payload, metadata):
        print("dbridges:rpc.server.connect.success", payload, metadata)
        #  Execute Server function with parameter 
        await self.summary()

    #  Bind to event server.connect.success, This will be triggered when the connection to server is successfull
    self.wiki.bind("dbridges:rpc.server.connect.success", serverconnect_success)

async def summary(self):
    try:
        #  Execute Server function with argument parameter .
        p = await self.wiki.call('summary', self.searchString, 10000, None)

        def onResult(res):
            #  Response will be received here. 
            print(res)

        def onError(res):
            #  Catch execption for .call
            print("Error", res.source, res.code, res.message, )

        p.then(onResult).catch(onError)

    except Exception as dberror:
        print("Exception: ", dberror.source, dberror.code, dberror.message)
// Connect to rpcServer
try
{
    wiki = await dbridge.rpc.connect("wiki.search");
}
catch (dBridges.exceptions.dBError err)
{
    // Catch connect exception
    Console.WriteLine("dBridge rpc.connect exception {0} {1} {2}", err.source, err.code, err.message);
}

// Bind to event server.connect.success, This will be triggered when the connection to server is success
Action<object, object> connetsuccess;
wiki.bind("dbridges:rpc.server.connect.success", connetsuccess = async (payload, metadata) => {
    // Execute Server function with parameter .
    IPromise<object> p = await wiki.call("summary", this.searchString, 1000 * 60 * 2, null);
    p.Then((response) => {
        Console.WriteLine("{1}", response);
    })
    .Catch((err) => {
        dBridges.exceptions.dBError er = err as dBridges.exceptions.dBError;
        Console.WriteLine("wiki => exception {0} , {1} , {2}",  er.source, er.code, er.message);
    });
});

// Bind to server.connect.fail. This will be triggered either if connection to rpcServer fails or reconnect fails.
Action<object, object> connetfail;
wiki.bind("dbridges:rpc.server.connect.fail", connetfail = (payload, metadata) => {
    dBridges.exceptions.dBError perror = payload as dBridges.exceptions.dBError;
    Console.WriteLine("dbridges:rpc.server.connect.fail {0} {1} {2}", perror.source, perror.code, perror.message);
});

Running this example

You need to change few lines of code in both the files to replace your dataBridges appKey, appSecret and Authentication URL.

Edit rpc.wiki.client and replace appkey and auth_url as shown below.

// Replace your dataBrdiges Application Key and Authentication URL below.
const dbApplnKey = 'appkey';
const dbAuthURL = 'auth_url';
// Replace your dataBrdiges Application Key and Authentication URL below.
self.dbApplnKey = 'appkey'
self.dbAuthURL = 'auth_url'
// Replace your dataBrdiges Application Key and Authentication URL below.
private string dbApplnKey = 'appkey';
private string dbAuthURL = 'auth_url';
Properties Description
auth_url (string) Client authentication url from dataBridges dashboard.
appkey (string) Client application Key from dataBridges dashboard.

Edit rpc.wiki.server and replace appkey ,appsecret and auth_url as shown below.

// Replace your dataBrdiges Application Key,Application Secret and Authentication URL below.
const dbApplnKey = 'appkey';
const dbAuthURL = 'auth_url';
const dbApplnSecret = 'appsecret';
// Replace your dataBrdiges Application Key and Authentication URL below.
self.dbApplnKey = 'appkey'
self.dbAuthURL = 'auth_url'
self.dbApplnSecret = 'appsecret'
// Replace your dataBrdiges Application Key and Authentication URL below.
private string dbApplnKey = 'appkey';
private string dbAuthURL = 'auth_url';
private string dbApplnSecret = 'appsecret';
Properties Description
auth_url (string) Server authentication url from dataBridges dashboard.
appkey (string) Server application Key from dataBridges dashboard.
appsecret (string) Server application Secret from dataBridges dashboard.

Depending on the language you need to run two different instance of client and server program to test the above example.

After starting the server application, start rpc.wiki.client with one argument i.e searchString for which the wiki information has to be retrieved.

Understanding application output

Upon starting Server application will give below output.

Connection Connected aovAKhcfI0ZU6TQW8o8ad.5TG2h_j2PEB7ZdytAInc
dbridges:rpc.server.registration.success
dbridges:rpc.server.online

rpc.server.online event when shown in the output indicates the server is ready to serve RPC calls.

Upon starting Client application will give below output depending on searchString passed in the argument parameter . For below example we have passed google as argument parameter.

{
  statuscode: 0,
  exectime: 1140,
  data: `Google LLC ( (listen)) is an American multinational technology company focusing on search engine technology, online advertising, cloud computing, computer software, quantum computing, e-commerce, artificial intelligence, and consumer electronics. It has been referred to as "the most powerful company in the world" and one of the world's most valuable brands due to its market dominance, data collection, and technological advantages in the area of artificial intelligence. Its parent company Alphabet is considered one of the Big Five American information technology companies, alongside Amazon, Apple, Meta, and Microsoft.\n` +
    "Google was founded on September 4, 1998, by Larry Page and Sergey Brin while they were PhD students at Stanford University in California. Together they own about 14% of its publicly listed shares and control 56% of the stockholder voting power through super-voting stock. The company went public via an initial public offering (IPO) in 2004. In 2015, Google was reorganized as a wholly owned subsidiary of Alphabet Inc. Google is Alphabet's largest subsidiary and is a holding company for Alphabet's Internet properties and interests. Sundar Pichai was appointed CEO of Google on October 24, 2015, replacing Larry Page, who became the CEO of Alphabet. On December 3, 2019, Pichai also became the CEO of Alphabet.The company has since rapidly grown to offer a multitude of products and services beyond Google Search, many of which hold dominant market positions. These products address a wide range of use cases, including email (Gmail), navigation (Waze & Maps), cloud computing (Cloud), web browsing (Chrome), video sharing (YouTube), productivity (Workspace), operating systems (Android), cloud storage (Drive), language translation (Translate), photo storage (Photos), video calling (Meet), smart home (Nest), smartphones (Pixel), wearable technology (Pixel Watch & Fitbit), music streaming (YouTube Music), video on demand (YouTube TV), artificial intelligence (Google Assistant), machine learning APIs (TensorFlow), AI chips (TPU), and more. Discontinued Google products include gaming (Stadia), Glass, Google+, Reader, Play Music, Nexus, Hangouts, and Inbox by Gmail.Google's other ventures outside of Internet services and consumer electronics include quantum computing (Sycamore), self-driving cars (Waymo, formerly the Google Self-Driving Car Project), smart cities (Sidewalk Labs), and transformer models (Google Brain).Google and YouTube are the two most visited websites worldwide followed by Facebook and Twitter. Google is also the largest search engine, mapping and navigation application, email provider, office suite, video sharing platform, photo and cloud storage provider, mobile operating system, web browser, ML framework, and AI virtual assistant provider in the world as measured by market share. On the list of most valuable brands, Google is ranked second by Forbes and fourth by Interbrand. It has received significant criticism involving issues such as privacy concerns, tax avoidance, censorship, search neutrality, antitrust and abuse of its monopoly position."
}