Querying tables by Tags

62 views
Skip to first unread message

Yogeswari Narayasamy

unread,
Aug 18, 2020, 5:51:46 AM8/18/20
to Tinode General
I have a requirement to query table Users by its "Tags".
For example, given the Tags = "mhub:08f38a9a-48e6-435c-be31-18731d4ac269", I need to return the "Id": "3fsC5QpNu8U".
How can I accomplish this?

Will the following approach fulfill this requirement?
I've written it based on the proto file:
My code
func search(w http.ResponseWriter, req *http.Request) {
   fmt.Println("in func search")
   enableCors(&w)

    crtFile := "/home/yogesnsamy/Coding/MHub/prod_cert/cert.crt"
   creds, err := credentials.NewClientTLSFromFile(crtFile, "")
   if err != nil {
       log.Fatal("Error loading cert", err)
   }

    // establish a new client connection to the grpc server
   cc, err := grpc.Dial("dev.mhub.my:16060", grpc.WithTransportCredentials(creds))
   if err != nil {
       log.Fatalf("Error connecting to: %v", err)
   }

    // close the connection when all ops are done
   defer cc.Close()
   // initiate a client
   c := pbx.NewPluginClient(cc)
   fmt.Printf("Created client: %f", c)
   //  Find(ctx context.Context, in *SearchQuery, opts ...grpc.CallOption) (*SearchFound, error)
   request := &pbx.SearchQuery{
UserId: "mhub:08f38a9a-48e6-435c-be31-18731d4ac269",
       Query:  "tags",
   }
   res, err := c.Find(context.Background(), request)
   if err != nil {
       log.Fatalf("error while calling Find RPC: %v", err)
   }
   log.Printf("Response from Find: %v", res.Status)

}
Is the definition of SearchQuery in the code above correct?
 request := &pbx.SearchQuery{
     
UserId: "mhub:08f38a9a-48e6-435c-be31-18731d4ac269",
     
Query:  "tags",
 
}



The code compiles fine. But following is the result on the client:
And on the http server serving the Find function, it says "error while calling Find RPC: rpc error: code = Unimplemented desc = unknown service pbx.Plugin
exit status 1"
:
Is this because Find is not implemented on the server side yet?
- How can I determine this (I'm not sure where to look)?
- If I need to implement PluginServer service to make this work, which files do I edit? How do I go about it?
Appreciate your guidance, thank you. 

Gene

unread,
Aug 18, 2020, 2:27:34 PM8/18/20
to Tinode General
You can search by tag directly:

out: {"set":{"id":"116748","topic":"fnd","desc":{"public":"xe...@example.com"}}} 
in: {"ctrl":{"id":"116748","topic":"fnd","code":200,"text":"ok","ts":"2020-08-18T18:25:19.152Z"}} 
out: {"get":{"id":"116749","topic":"fnd","what":"sub"}} 
in: {"meta":{"id":"116749","topic":"fnd","ts":"2020-08-18T18:25:19.254Z","sub":[{"updated":"2020-08-12T21:15:31.018Z","acs":{"mode":"JRWPAS"},"public":{"fn":"Xena Pacifist Peasant","photo":{"data":"<22080, bytes: /9j/4AAQSkZJ...dua0V563Z//Z>","type":"jpg"}},"private":["email:xe...@example.com"],"user":"usrMStLaSNWKow"}]}} 

Instead of tag 'xe...@example.com' use your tag 'mhub:08f38a9a-48e6-435c-be31-18731d4ac269'

Yogeswari Narayasamy

unread,
Aug 19, 2020, 7:33:35 AM8/19/20
to Tinode General
Thank you for the pointer. I tried to simulate how the web app works and wrote the following code, but it doesn't return the result the web app does.

fmt.Println("in func get")
   enableCors(&w)

    crtFile := "/home/yogesnsamy/Coding/MHub/prod_cert/cert.crt"
   creds, err := credentials.NewClientTLSFromFile(crtFile, "")
   if err != nil {
       log.Fatal("Error loading cert", err)
   }

    conn, err := grpc.Dial("dev.mhub.my:16060", grpc.WithTransportCredentials(creds))
   if err != nil {
       log.Fatal("Error dialing", err)
   }

    c := pbx.NewNodeClient(conn)
   response, err := c.MessageLoop(context.Background())

    if err != nil {
       log.Fatal("Error calling: ", err)
   }

    hi := &pbx.ClientHi{}
   hi.Id = "1"
   hi.UserAgent = "Golang_Spider_Bot/3.0"
   hi.Ver = "0.15"
   hi.Lang = "EN"

    msgHi := &pbx.ClientMsg_Hi{Hi: hi}
   clientMessage := &pbx.ClientMsg{Message: msgHi}
   err = response.Send(clientMessage)

    if err != nil {
       log.Fatal("error sending message ", err)
   }

    serverMsg, err := response.Recv()
   if err != nil {
       log.Fatal(err)
   }

    // LOGIN as Admin user
   // NOTE: you will need to create an admin user account
   // Upgrade an existing user to have admin access
   login := &pbx.ClientLogin{}
   login.Id = "2"
   login.Scheme = "basic"
   login.Secret = []byte("xena:xena123") // normal user is also able to create a group
   clMsg := &pbx.ClientMsg_Login{Login: login}
   clientMessage = &pbx.ClientMsg{Message: clMsg}
   err = response.Send(clientMessage)

    if err != nil {
       log.Fatal("error sending message ", err)
   }

    serverMsg, err = response.Recv()
   if err != nil {
       log.Fatal(err)
   }

    clientSet := &pbx.ClientSet{
       Id:    "888",
       Topic: "fnd",
       Query: &pbx.SetQuery{
           Desc: &pbx.SetDesc{
               Public: []byte(`{"public":"email:al...@example.com"}`),
           },
       },
   }
   clSet := &pbx.ClientMsg_Set{Set: clientSet}
   setMessage := &pbx.ClientMsg{
       Message: clSet,
   }
   err = response.Send(setMessage)
   if err != nil {
       log.Fatal(err)
   }

    serverMsg, err = response.Recv()
   fmt.Println("1 >> ", serverMsg)

    if err != nil {
       log.Fatal(err)
   }
   code := serverMsg.GetCtrl().Code

    if code == 200 {
       clientGet := &pbx.ClientGet{
           Id:    "999",
           Topic: "fnd",
           Query: &pbx.GetQuery{
               What: "sub",
           },
       }
       clGet := &pbx.ClientMsg_Get{Get: clientGet}
       getMessage := &pbx.ClientMsg{
           Message:    clGet,
           OnBehalfOf: "usrkmKwvgHGUrQ",
       }
       err = response.Send(getMessage)
       if err != nil {
           log.Fatal(err)
       }

        serverMsg, err = response.Recv()

        if err != nil {
           log.Fatal(err)
       }
       fmt.Println(serverMsg)
   }
The result I note on the browser when look for user Alice:
- set
- then get
The result from my code is like the following:

Could you advise on what I'm not doing correctly? Thank you.

Gene S

unread,
Aug 19, 2020, 11:26:27 AM8/19/20
to tinode
You are searching for 


You want to search for email:al...@example.com or even just al...@example.com



--
You received this message because you are subscribed to the Google Groups "Tinode General" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tinode+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tinode/7a5c469f-0eda-46c0-9e74-7720ab55c399o%40googlegroups.com.

Yogeswari Narayasamy

unread,
Aug 19, 2020, 12:21:23 PM8/19/20
to Tinode General
I cleaned up the code by referring to the bi-directional resource you shared earlier. Thank you for that.

Referring to your comment:
You want to search for email...@example.com or even just al...@example.com
I tried a few variations of this to no avail:
// Public: []byte(`{"public":"email:al...@example.com"}`),
// Public: []byte("al...@example.com"),
Public: []byte(`email:al...@example.com`),

Could you share the right way of defining this?

func get(w http.ResponseWriter, req *http.Request) {
   fmt.Println("Starting to do a BiDi Streaming RPC...")
   crtFile := "/home/yogesnsamy/Coding/MHub/prod_cert/cert.crt"
   creds, err := credentials.NewClientTLSFromFile(crtFile, "")
   if err != nil {
       log.Fatal("Error loading cert", err)
   }

    conn, err := grpc.Dial("dev.mhub.my:16060", grpc.WithTransportCredentials(creds))
   if err != nil {
       log.Fatal("Error dialing", err)
   }

    c := pbx.NewNodeClient(conn)
   // we create a stream by invoking the client
   stream, err := c.MessageLoop(context.Background())
   if err != nil {
       log.Fatalf("Error while creating stream: %v", err)
       return
   }

    requests := []*pbx.ClientMsg{
       &pbx.ClientMsg{
           Message: &pbx.ClientMsg_Hi{Hi: &pbx.ClientHi{
               Id:        "1",
               UserAgent: "Golang_Spider_Bot/3.0",
               Ver:       "0.15",
               Lang:      "EN",
           }},
       },
       &pbx.ClientMsg{
           Message: &pbx.ClientMsg_Login{Login: &pbx.ClientLogin{
               Id:     "2",
               Scheme: "basic",
               Secret: []byte("xena:xena123"),
           }},
       },
       &pbx.ClientMsg{
           Message: &pbx.ClientMsg_Set{Set: &pbx.ClientSet{
               Id:    "3",
               Topic: "fnd",
               Query: &pbx.SetQuery{
                   Desc: &pbx.SetDesc{
                       // Public: []byte(`{"public":"email:al...@example.com"}`),
                       // Public: []byte("al...@example.com"),
                       Public: []byte(`email:al...@example.com`),
                   },
               },
           }},
       },
       &pbx.ClientMsg{
           Message: &pbx.ClientMsg_Get{Get: &pbx.ClientGet{
               Id:    "4",
               Topic: "fnd",
               Query: &pbx.GetQuery{
                   What: "sub",
               },
           }},
       },
   }

    waitc := make(chan struct{})
   // we send a bunch of messages to the client (go routine)
   go func() {
       // function to send a bunch of messages
       for _, req := range requests {
           fmt.Printf("Sending message: %v\n", req)
           stream.Send(req)
           time.Sleep(1000 * time.Millisecond)
       }
       stream.CloseSend()
   }()
   // we receive a bunch of messages from the client (go routine)
   go func() {
       // function to receive a bunch of messages
       for {
           res, err := stream.Recv()
           if err == io.EOF {
               break
           }
           if err != nil {
               log.Fatalf("Error while receiving: %v", err)
               break
           }
           fmt.Printf("Received: %v\n", res.GetMessage())
       }
       close(waitc)
   }()

    // block until everything is done
   <-waitc
}

My result:

On Wednesday, 19 August 2020 23:26:27 UTC+8, Gene wrote:

You want to search for email...@example.com or even just al...@example.com



To unsubscribe from this group and stop receiving emails from it, send an email to tin...@googlegroups.com.

Gene S

unread,
Aug 19, 2020, 8:35:56 PM8/19/20
to tinode
On Wed, Aug 19, 2020 at 9:21 AM Yogeswari Narayasamy <yogeswari....@gmail.com> wrote:
I cleaned up the code by referring to the bi-directional resource you shared earlier. Thank you for that.

Referring to your comment:
You want to search for email...@example.com or even just al...@example.com
I tried a few variations of this to no avail:
// Public: []byte(`{"public":"email:al...@example.com"}`),
// Public: []byte("al...@example.com"),
Public: []byte(`email:al...@example.com`),

Could you share the right way of defining this?

To unsubscribe from this group and stop receiving emails from it, send an email to tinode+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tinode/57a380dd-ec4c-41f5-8a61-d90c1c5987a8o%40googlegroups.com.
Message has been deleted

Yogeswari Narayasamy

unread,
Aug 20, 2020, 9:29:19 PM8/20/20
to Tinode General
I'm encountering an issue in running this code; I'm only getting the expected result if I'm first logged in to the web app and go to the "Find" screen.

In the example below, when I'm not logged in to the front-end and call the service, I don't get the expected result:

Even when I log in to the front-end app first then make the same call, I don't get the expected result:

It's only when I'm on the 'FIND' screen on the web app the code works fine:


Why is this happening? How can I get my code to work? Sharing the code below for your kind reference:

package main

import (
   "context"
   "fmt"
   "io"
   "log"
   "net/http"
   "time"

)
                       // Public: []byte(`"email:al...@example.com"`),
                       Public: []byte(`"mhub:e995b6be-b528-4d1b-9e52-cdcca7ebf4d6"`),
                   },
               },
           },
           },
       },
       &pbx.ClientMsg{
           OnBehalfOf: "usrkmKwvgHGUrQ", // xena - no impact even if commented out
func main() {
   http.HandleFunc("/get", get)
   http.ListenAndServe(":8090", nil)
}





On Thursday, 20 August 2020 21:45:36 UTC+8, Yogeswari Narayasamy wrote:
Received: &{id:"4" topic:"fnd" sub:<updated_at:1587733349014 acs:<> public:"{\"fn\":\"Alice Johnson\",\"photo\":{\"data\":\"/9j/4AAQSkZJRgABAQEASABIAAD/..==\",\"type\":\"jpg\"}}" private:"[\"email:alic....ple.com\"]" user_id:"usrRpP-hz3gYmw" > }

The result above is retrieved by
res.GetMessage()
May I know how to retrieve the value of user_id from the above?


On Thursday, 20 August 2020 08:35:56 UTC+8, Gene wrote:


On Wed, Aug 19, 2020 at 9:21 AM Yogeswari Narayasamy <yogeswari...@gmail.com> wrote:
I cleaned up the code by referring to the bi-directional resource you shared earlier. Thank you for that.

Referring to your comment:
You want to search for email...@example.com or even just al...@example.com
I tried a few variations of this to no avail:
// Public: []byte(`{"public":"email:al...@example.com"}`),
// Public: []byte("al...@example.com"),
Public: []byte(`email...@example.com`),

Could you share the right way of defining this?

It's either `"email...@example.com"` or just `email...@example.com`.
 

                       Public: []byte(`email...@example.com`),

My result:
Message has been deleted

Gene Sokolov

unread,
Aug 21, 2020, 12:45:50 PM8/21/20
to tin...@googlegroups.com
You must subscribe to 'fnd' first.

On Thu, Aug 20, 2020 at 9:19 PM Yogeswari Narayasamy <yogeswari....@gmail.com> wrote:
Or does this indicate that I've to run the code multiple times to get the expected result? Because right it works even when I close the web app. Sorry for these questions, they sound weird even to myself.
To unsubscribe from this group and stop receiving emails from it, send an email to tinode+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tinode/85f6cee0-c793-46fb-a4ed-eabd9736b30fo%40googlegroups.com.

Yogeswari Narayasamy

unread,
Aug 23, 2020, 10:15:14 PM8/23/20
to Tinode General
I modified the code to imitate these actions:

    requests := []*pbx.ClientMsg{
       &pbx.ClientMsg{
           Message: &pbx.ClientMsg_Hi{Hi: &pbx.ClientHi{
               Id:        "1",
               UserAgent: "Golang_Spider_Bot/3.0",
               Ver:       "0.15",
               Lang:      "EN",
           }},
       },
       &pbx.ClientMsg{
           Message: &pbx.ClientMsg_Login{Login: &pbx.ClientLogin{
               Id:     "2",
               Scheme: "basic",
               Secret: []byte("carol:carol123"),
           }},
       },
       &pbx.ClientMsg{
           Message: &pbx.ClientMsg_Sub{
               Sub: &pbx.ClientSub{
                   Id:    "3",
                   Topic: "fnd",
                   GetQuery: &pbx.GetQuery{
                       What: "sub",
                   },
               },
           },
       },
       &pbx.ClientMsg{
           Message: &pbx.ClientMsg_Set{Set: &pbx.ClientSet{
               Id:    "3",
               Topic: "fnd",
               Query: &pbx.SetQuery{
                   Desc: &pbx.SetDesc{
                       // Public: []byte(`"email:al...@example.com"`),
                       // Public: []byte(`"mhub:e995b6be-b528-4d1b-9e52-cdcca7ebf4d6"`),
                       Public: []byte(`"mhub:"+cuid[0]`),
                   },
               },
           },
           },
       },
       &pbx.ClientMsg{
           // OnBehalfOf: "usrkmKwvgHGUrQ", // xena - no impact even if commented out
           Message: &pbx.ClientMsg_Get{Get: &pbx.ClientGet{
               Id:    "4",
               Topic: "fnd",
               Query: &pbx.GetQuery{
                   What: "sub",
               },
           }},
       },
   }
But I'm getting a list of Carol's contacts instead of the result for my keyword search ("mhub:...."). Could you please assist to correct my code?


On Saturday, 22 August 2020 00:45:50 UTC+8, Gene wrote:
You must subscribe to 'fnd' first.

                       // Public: []byte(`"email...@example.com"`),

Gene S

unread,
Aug 23, 2020, 11:57:31 PM8/23/20
to tinode
Here is what you are doing:

id 3: subscribe to fnd and request default results
id 3: set your search query (you are reusing id 3 which you should not do)
id 4: request search results.

Then you are getting results. You do not show your log, so I'm guessing:

ctrl id=3 confirming subscription to fnd
meta id=3 default search results which you call "list of Carol's contacts"
ctrl id=3 for your set request
meta id=4 your expected search results "the result for my keyword search ("mhub:....")"


To unsubscribe from this group and stop receiving emails from it, send an email to tinode+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tinode/970e238b-2a65-4f30-a11f-772eccab623bo%40googlegroups.com.

Yogeswari Narayasamy

unread,
Aug 24, 2020, 3:04:20 AM8/24/20
to Tinode General
Thank you, I'm getting the expected result after correcting the id.

On Monday, 24 August 2020 11:57:31 UTC+8, Gene wrote:
Here is what you are doing:

id 3: subscribe to fnd and request default results
id 3: set your search query (you are reusing id 3 which you should not do)
id 4: request search results.

Then you are getting results. You do not show your log, so I'm guessing:

ctrl id=3 confirming subscription to fnd
meta id=3 default search results which you call "list of Carol's contacts"
ctrl id=3 for your set request
meta id=4 your expected search results "the result for my keyword search ("mhub:....")"


                       // Public: []byte(`"email...@example.com"`),
Reply all
Reply to author
Forward
0 new messages