Services with Echo

Hello,


I don't find any examples and documentation about the usage of services with the Echo.
Can you describe that?

Hello Alex,

Which "services" are you referring to, exactly? 

How to implement XData Auth demo with Echo enabled?

There is a demo in Echo install (<echo_install_dir>\demos\xdata) that shows how to use Echo with TMS XData. Then if you want to protect the server, just add a JWT middleware to it and only clients sending a valid JWT token will be able to access the server. The process of issuing the token is the same for other servers, and is described here:


http://www.tmssoftware.biz/business/xdata/doc/web/authentication_example_using_j.html

Ok, and is it my question: in offline mode will be impossible to work with JWT?

In other words: no offline more if we have JWT middleware?

I don't see the relation between "offline" and "JWT middleware". Two very different things.

Working "offline" means data will be save locally and you can later replicate to a server.
"JWT middleware" is a feature you add to your server to require authentication to its services. If you mean JWT middleware in the server that will receive data for replication, you can use it, when the client that saved data offline wants to send data to the server, it authenticates with the server. It will only need to authenticate when connecting to the server, of course, so there is no relation with "offline"

How to determine, when customer need authenticate on the server?

He will need to authenticate to the server when he needs to connect to the server, i.e., when he needs to send the batches of data to be replicated to the server ("push" data).

Hello again,


Currently I'm have such implementation of Echo server:


var
  IndyServer: TIndySparkleHTTPServer;
  Echo: TEcho;
  Timer: TSparkleTimer;


procedure ServerStartEcho(const APort: string);
var
  EchoModule: TEchoServerModule;
  Pool: IDBConnectionPool;
  NodeManager: IEchoNodeManager;
  Conn: IDBConnection;
  Encrypt: IEncryptMiddleware;
begin
  if Assigned(IndyServer) then
     Exit;


  IndyServer := TIndySparkleHTTPServer.Create;


  Pool := TDBConnectionPool.Create(50, TDBConnectionFactory.Create(
    function: IDBConnection
    var
      FDConnection: TFDConnection;
      Connection: TAureliusConnection;
    begin
      FDConnection := TFDConnection.Create(nil);
      FDConnection.Params.Values['Database'] := 'C:\Temp\Test.sdb';
      FDConnection.Params.Values['DriverID'] := 'SQLite';
      FDConnection.Params.Values['Encrypt']  := 'enAes_256';
      FDConnection.Params.Values['UserName'] := 'UserName';
      FDConnection.Params.Values['Password'] := 'Password';
      Connection := TAureliusConnection.Create(nil);
      Connection.AdaptedConnection := FDConnection;
      Connection.AdapterName := 'FireDac';
      Connection.SQLDialect := 'SQLite';
      Result := Connection.CreateConnection;
    end));
  EchoModule := TEchoServerModule.Create('http://+:' + APort + '/tms/echo', Pool);


  EchoModule.AddMiddleware(TJwtMiddleware.Create('secret', False, True));


  IndyServer.Dispatcher.AddModule(EchoModule);


  Echo := TEcho.Create(Pool);


  {$IFDEF MSWINDOWS}if not IsDebuggerPresent then{$ENDIF}
  begin
    Conn := Echo.Pool.GetConnection;
    TDatabaseManager.Update(Conn);
    TDatabaseManager.Update(Conn, TEcho.Explorer);
    Conn := nil;
  end;


  // Get the NodeManager instance
  NodeManager := Echo.GetNodeManager;


  if NodeManager.SelfNode = nil then begin
    NodeManager.CreateNode('server');
    NodeManager.DefineSelfNode('server');
  end;


  Timer := TSparkleTimer.Create(
    procedure(Echo: TObject)
    begin
      TEcho(Echo).BatchLoad;
      TEcho(Echo).Route;
    end,
    Echo, 2000, TTimerType.Periodic);


  IndyServer.Active := True;
end;


I can't use this server module for connecting no my services?
Or this is possible?
Can't find any info in documentation

Something like this:

  XClient := TXDataClient.Create;

  XClient.Uri := 'http://localhost:' + APort + '/tms/echo';
  Token := XClient.Service<ILoginService>.Login('testuser', 'testuser');


Or I shoult create another server module with another Uri for this?
And also: my server code is correct and doesn't contain resource leaking?
  1. I suppose you are destroying IndyServer, Echo and Timer at the end of your application?

    2. You are leaking both FDConnection and Connection. Since you are creating it manually, don't use Connection (TAureliusConnection). Just create the FDConnection and return IDBConnection using TFireDacConnectionAdapter using True as the second parameter: http://www.tmssoftware.biz/business/aurelius/doc/web/component_adapters.html

    3. Make sure the timer doesn't perform twice at same time. BatchLoad is not "thread-safe". Set a flag before the processing and reset at the end, and do not execute the timer code if the flag is set.

    4. For extra service operations, use XData a module.