RTC Forums
May 08, 2024, 06:45:01 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Question about gateways  (Read 5785 times)
Pieter
RTC Expired
*
Posts: 17


« on: August 05, 2016, 09:24:44 PM »

Hi Danijel,

I’m working on a client/server application but I am a novice so I would like to ask you to point me in the right direction. Listed below are the application functionality and current setup:

Functionality
  • Client can request specific information from the Server using remote functions
  • Client can upload a file to the Server using a data request
  • Server-initiated sending of identical information to all connected Clients

Server setup
  • TRtcHttpServer (MultiThreaded) on main form
     DataModule contains:
  • TRtcServerModule (AutoSessions)
  • TRtcFunctionGroup with several TRtcFunctions
  • TRtcDataProvider

Client setup
  • TRtcHttpClient (not MultiThreaded)
  • TRtcClientModule (AutoSessions, AutoSyncEvents)
  • TRtcDataRequest (AutoSyncEvents)
  • TRtcResult

So far everything works fine. I just need to get the Server to send certain information to all connected Clients when said information becomes available.
From what I have read about Delayed Calls and Gateways I gather that using a Gateway might be the best option.

Is it possible/advisable to use a Gateway for this?
And if so:

1. Should I use TRtcGateway or TRtcHttpGateway?
2. Should I use TRtcGateClient or TRtcHttpGateClient?
3. Will each client have three connections to the server when both TRtcHttpClient and TRtcGateClient (or TRtcHttpGateClient) are used?
4. Could you include a code example of the Server sending information (of any type) to all connected Clients via a Gateway?

Thank you in advance for your kind reply,
Pieter van Gelder
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: August 06, 2016, 10:16:57 AM »

1. Since you already have a working RTC Server, I'd recommend using a TRtcGateway component with your existing TRtcHttpServer component, so you don't have to open a separate port to add Gateway functionaliy to your Server.

2. Use the TRtcHttpGateClient component (TRtcGateClient component does not exist).

3. Every TRtcHttpGateClient component uses 2 connections (one for sending and one for receiving data). If you also have a TRtcHttpClient component in your Client Project, adding one TRtcHttpGateClient component and connecting to the same Server will result in 3 connections open to the same Server, which is the limit when using the WinInet API on Windows (useProxy=TRUE).

4. Since the RTC Gateway component does NOT have any options for direct communication with connected Gate Clients (it only serves as a mediator between all the Clients), if you want your Server to communicate with one or more connected Gate Clients through the Gateway, use a TRtcHttpGateClient component in your Server Project. It makes no difference for the Gateway where the connection is coming from (it can also be from inside the same process). For an example on using TRtcGateway and TRtcHttpGateClient components, take a look at the ChatServer and ChatClient Projects in the SDKDemos_VCL Project Group.

To distinguish between the TRtcHttpGateClient component used by your Server and TRtcHttpGateClient components used by your Clients, use the "GateUserAuth" property of the TRtcHttpGateClient component, which is made available in all TRtcGateway components events as the "UserAuth" parameter and can be used to authenticate each Client and the Server (also using a TRtcHttpGateClient component) when logging on to the Gateway.

Since each Gate Client (including the one used by your Server) gets a new random UserID when logging on to the Gateway, for Clients and the Server to "find" each other, you can use one of the available "notification" mechanisms provided by the TRtcGateway component. For example, the "AddUserToChannel" method, as shown in the ChatServer and ChatClient examples.

Best Regards,
Danijel Tkalcec
Logged
Pieter
RTC Expired
*
Posts: 17


« Reply #2 on: August 06, 2016, 11:24:29 AM »

Hi Danijel,

Thank you for your quick and detailed reply.
I will implement the gateway as you suggest.

Thanks again and best regards,
Pieter
Logged
Pieter
RTC Expired
*
Posts: 17


« Reply #3 on: August 10, 2016, 05:32:10 PM »

Hi Danijel,

I added a Gateway and HttpGateClient to the server and a HttpGateClient to the client.
To test this I've done the following:

Gateway UserReady event:

   GateCli.AddUserToMyGroup(1, UserID) returns True
   GateCli.IsUserInMyGroup(1, UserID) returns True
   GateCli.UsersInMyGroup(1) returns 1
   GateCli.SendBytes(UserID, 1, 100, 'Testing') returns True

SendBytes fires DataReceivedGUI on the client:

   Data.ToBuffer := True;
   Log('CallID = ' + Int2Str(Data.CallID)); // on-screen logging function (not rtcLog)
   if Data.CallID = 100 then
      GateCli.AddFriend(Data.UserID);
   Log(RtcBytesToString(Data.Content));

So far this works fine. I then start a timer which sends a group message:

   GateCli.SendToMyGroup(1, 200, 'Message to all')

This command also returns True but DataReceivedGUI does not fire on the client.
Can you please tell me what I'm doing wrong?

Best regards,
Pieter
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #4 on: August 11, 2016, 08:58:41 AM »

1. Before you can use the "AddUserToMyGroup" or "AddUserToGroup" method on the "TRtcHttpGateClient" component, the user which you want to add to your Group has to add you as a friend on the Gateway. By using the "AddUserToMyGroup" or "AddUserToGroup" method BEFORE the user has added you to his friends list on the Geteway, even though your method calls will first succeed locally on the Client (provided the Client is connected to the Gateway and ready to send), the Gateway will refuse to execute your "AddUserToGroup" command (User will NOT be added to your Group on the Gateway) and a notification will be sent to your Client about this failure. ONLY AFTER your Client receives this notification from the Gateway, it will remove the User from its local User Group and THEN your calls to "SendToMyGroup" and "IsUserInMyGroup" on the Client will return FALSE.

2. By implementing your "DataReceivedGUI" event on the Client to add ANY user as a Friend who sends a message with CallID=100, you are disabling the security mechanism provided by the Gateway (see above), making your Gateway and Gate Clients vulnerable again.

3. Because any method calls executed directly on the TRtcGateway and TRtcHttpGateway components are considered safe, the security feature mentioned above does NOT apply to method calls made directly on the TRtcHttpGateway component. In other words, instead of using the "GateCli".AddUserToMyGroup" method and asking the remote Client to add you as a Friend, since your "GateCli" is actually your Server and not some random Client, you can use the "AddUserToGroup" method directly on the "TRtcGateway" component from inside the "UserReady" event and do NOT need to ask the user to add you as a Friend. To send messages to all these users, use the "SendBytes" method with your UserID (=GateCli.MyUID in your example) and the GroupID used in the AddUserToGroup method (=1 in your Example).

4. I'm not sure if you are aware of this, but if you do NOT implement the "OnDataFilter" event on your TRtcHttpGateClient component by setting "Wanted:=TRUE" ONLY when you want the "OnDataReceived" and/or "OnDataReceivedGUI" event to be triggered and you do NOT implement the "OnDataReceived" event by setting the "WantGUI:=TRUE" ONLY when you need access to the GUI, your "OnDataReceivedGUI" event will be triggered and syhcnronized with the Main Thread for every single chunk of data received from the Gateway.

Best Regards,
Danijel Tkalcec
Logged
Pieter
RTC Expired
*
Posts: 17


« Reply #5 on: August 11, 2016, 01:11:02 PM »

Thank you again for your great support, Danijel.
It's all clear to me now and it's working perfectly.

Best regards,
Pieter
Logged
ISIGest
RTC Expired
*
Posts: 121


« Reply #6 on: August 12, 2016, 01:56:46 PM »

Can I also send some data or notification from server direct to GroudID or UserID?
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #7 on: August 13, 2016, 06:39:40 AM »

Data can be sent to any UserID and GroupID with the SendBytes method on the TRtcHttpGateClient component.

To send to a single user, UserId is the receiver and GroupID is 0 (zero).

To send to your group, UserId is your own ID (MyUID) and GroupID is the receiver group, same as used in the AddUserToGroup method to add users to your group.

To send data to a single user and provide the "Data.ToGroupID" parameter, UserID parameter is the receiver and GroupID parameter is whatever the Data.ToGroupID property should be on the receiver side.

You can NOT send to a Group owned by another Gate Client, though. Only group owners can send data to their groups.

Best Regards,
Danijel Tkalcec

Logged
dex
RTC Expired
*
Posts: 10


« Reply #8 on: September 21, 2016, 10:28:39 AM »

Hi Daniel,

Described setup is really interesting, and I am thinking of using it for project which would include clients running on the mobile devices.
In my case, clients would also use remote functions, and there is a need for messaging functionality (primarily for sending new info from server to clients, while exchanging messages between clients is a plus but not a must).

The fact that 3 connections are used makes me think of possible increased battery consumption and network traffic.
So in case clients are running on mobile devices, would you recommend using above described setup or there is an even better option?

Regards,
Dejan
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #9 on: September 22, 2016, 06:10:17 PM »

What I recommend is to try it out. Make a simple Client and Server Project and test how it works. Some things might sound good on paper, but you won't know for sure until you've ran your Application on the device(s) you are targeting. This is especially true for mobile devices running Android and iOS.

Best Regards,
Danijel Tkalcec
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.029 seconds with 18 queries.