Common code
The common
sources set is the place where you define how your remote services should work. You can define as many services as you wish, and they can have as many methods as you need. It's a good practice to split your services based on their context and functions.
Declaring an interface
Designing the interface is probably the most important step, and during this process you have to stick to some important rules.
An interface must be annotated with @RpcService
annotation.
@RpcService
annotation.Kilua RPC compiler plugin will generate common, backend and frontend code based on the interface name.
A method must be suspending
Kotlin coroutines allow the framework to translate asynchronous calls into synchronous-like code.
A method must have from zero to six parameters
This is the restriction of the current version of the library. It may change in the future.
A method can't return nullable value
You will get a compilation error if this rule is not met.
Method parameters and return value must be of supported types
all basic Kotlin types (
String
,Boolean
,Int
,Long
,Short
,Char
,Byte
,Float
,Double
)Enum
classes defined in common code and annotated with@Serializable
annotationAll date and time types from
kotlinx-datetime
libraryA
dev.kilua.rpc.types.Decimal
type, which is automatically mapped toDouble
on the frontend side andjava.math.BigDecimal
on the backend sideany class defined in common code with a
@Serializable
annotationa
List<T>
, where T is one of the above typesa
T?
, where T is one of the above types (allowed only as method parameters - see previous rule)a
Result<T>
, where T is one of the above types, can be used as a method return valuea
Unit
can be used as a method return value.
Even with the above restrictions, the set of supported types is quite rich and you should be able to model almost any use case for your applications. You can always wrap any data structure into a serializable data class. It's also a simple way to pass around the parameters count limit.
With an interface defined in the common code, the type safety of your whole application is forced at compile time. Any incompatibility between the frontend and the backend code will be marked as a compile-time error.
Using annotations to change HTTP method and/or route name
By default Kilua RPC will use HTTP POST server calls and automatically generated route names. But you can use @RpcBinding
, @RpcBindingMethod
and @RpcBindingRoute
annotations on any interface method, to change the default values.
Last updated