B4A Class [B4X] Remote MongoDB Connector (RMDBC)

Discussion in 'Additional libraries, classes and official updates' started by Erel, Oct 23, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This is a similar solution to RDC that works with MongoDB databases.
    MongoDB is a simple and powerful NoSQL database. It is a bit like KVS (key value store) class. However unlike KVS which is a simple data store, MongoDB is a full database with powerful analytics features.

    The B4J server acts as a middleware between the clients and the database.
    B4J MongoDB library: https://www.b4x.com/android/forum/threads/mongodb-documents-database.72160/

    There is nothing to configure in the server side except of the server port. The server code is simple and
    can be extended as needed.

    Unlike RDC where the queries are set in the server side, in RMDBC the commands are set in the clients. This means that this solution as-is is more appropriate for trusted environments. You can add validation code to the server to make it more secure.

    Client code

    1. Add the types declaration to the main module:
    Code:
    Type MDBCommand (Collection As String, Maps As List, Method As String, Limit As Int)
    Type MDBResult (Success As Boolean, Tag As Object)
    2. Add MDBRequestManager class to your project. It depends on OkHttpUtils2 and RandomAccessFile library.
    Note that it is compatible with B4A, B4i and B4J.

    3. Initialize the manager:
    Code:
    manager.Initialize(Me, "mdb""http://192.168.0.6:51042/rmdbc"'RMDBC server ip and port
    4. Handle the events:
    Code:
    Sub JobDone (j As HttpJob)
       
    If manager.HandleJob(j) Then Return
       
    'handle other HttpJobs here
    End Sub

    Public Sub MDB_NonFindResult (Result As MDBResult)
       
    Log("Command completed. Success =" & Result.Success)
       
    If Result.Success Then GetResults
    End Sub

    Public Sub MDB_FindResult (Result As MDBResult, Documents As List)
       
    If Result.Success Then
         ListView1.Clear
         
    For Each m As Map In Documents
           ListView1.AddSingleLine(m.Get(
    "_id") & ": " & m.Get("total"))
         
    Next
       
    End If
    End Sub
    See the attached B4A and B4i client examples:


    upload_2016-10-23_16-56-28.png upload_2016-10-23_16-56-35.png
     

    Attached Files:

    ocalle, asales, Mashiane and 13 others like this.
  2. MarcoRome

    MarcoRome Expert Licensed User

    Great work Erel ;)
     
  3. johndb

    johndb Active Member Licensed User

    Is it possible to encrypt the client-server communications using the encryption features of RandomAccessFile?
     
    MarcoRome likes this.
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can use B4XCipher to encrypt the messages. However a better solution is to communicate with the RMDBC server over SSL.
     
    alimanam3386, Daniel-White and johndb like this.
  5. leitor79

    leitor79 Active Member Licensed User

    Thank you very much for sharing this!

    I'm getting red messages in the log screen and no window is shown. Is this an exception?


    Regards!
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    No. These messages always appear in jServer apps.

    There is no window because it is a non-ui project (and it must be non-ui).
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice