Kotlin and SmartFoxServer 2X

You may have heard of the Kotlin language before, but recently it has seen a big spike in popularity thanks to Google officially adopting it for Android development, alongside Java and C++.

Kotlin is an interesting statically typed language, developed by the good folks at Jetbrains, that shares many similarities with the likes of Scala and Swift. In a sense, it could be thought of the equivalent of Swift for the Java platform: a more modern, concise and safer language that runs in the JVM, fully interoperable with Java.

If that’s not enough, Kotlin also aims at compiling for other targets beyond the JVM, such as Javascript and native platforms and seeks to support asynchronous programming via coroutines and non blocking I/O.

In the context of using Kotlin with SmartFoxServer nothing would be easier: the integration on both server side and client side is painless and it works out of the box with any SFS2X release.

Let’s take a closer look, shall we?

» Server side

So what does it take exactly to write a server side Extension with Kotlin? The easiest way to do it is via Jetbrain’s IntelliJ, a commercial multi-language IDE that also comes as a “Community Edition”, free of charge.

This is very good news for anyone interested in experimenting with Kotlin; so we downloaded a copy, created a new Kotlin Project and added the basic jar files (sfs2x.jar, sfs2x-core.jar) to it.

setting libraries

This is done by right-clicking the project name » Open Module Settings » Libraries. From there you can click the plus sign (+) at the top of the second column and navigate to your jar files folder (i.e. SFS2X/lib/).

Nothing new up to this point besides maybe learning how to use IntelliJ, if you’re not familiar with it. Next comes the interesting part: writing some actual Kotlin code.

package sfs2x.kt

import com.smartfoxserver.v2.core.ISFSEvent
import com.smartfoxserver.v2.core.SFSEventType
import com.smartfoxserver.v2.extensions.BaseServerEventHandler
import com.smartfoxserver.v2.extensions.SFSExtension

class EventHandler : BaseServerEventHandler()
{
    override fun handleServerEvent(event: ISFSEvent?)
    {
        trace("Event received >> " + event?.type)
    }
}

class KotlinTestExtension : SFSExtension()
{
    override  fun init()
    {
        trace("Kotlin Extension running in Zone: " + parentZone.toString())

        // Add server event handler
        addEventHandler(SFSEventType.SERVER_READY, EventHandler())
    }

    override fun destroy()
    {
        super.destroy()

        trace("Extension shutting down");
    }
}

At the top we have our standard package declaration, just like in Java. Next a few imports and finally we encounter the first new concept: multiple classes in the same file!
Kotlin, as many recent languages, doesn’t force you in the Java’s one-class-per-file philosophy, so we can put two (or more) package level classes in the same source file.

Our main Extension class, KotlinTestExtension, extends the base SFSExtension class using the colon (:) notation, like in C#/C++. Then we proceed with overriding the init() and destroy() methods, declared with the Kotlin fun keyword, short for function of course.

Notice how the Java getParentZone() Extension method becomes parentZone (no method invocation): this is because in Kotlin there are no getters and setters, but only properties. This in turn means that all java beans exposed in SFS2X SDK will auto-complete to their property names, rather than their getter/setter names.

Also note how instantiating the EventHandler class does not require the “new” keyword, akin to languages such as Python.

Another bit worth of notice is this line from the EventHandler class:

trace("Event received >> " + event?.type)

If you’re familiar with languages such as Groovy or Swift you’ll recognize it as the null safe operator, which is exactly what this is. A useful compile-time help to avoid those nasty null pointer exceptions.

Finally we can generate our jar file and deploy it to SmartFoxServer.  To do so, let’s open the Module settings again via right-clicking the project name » Open Module Settings » Artifacts.

kot-ext-deploy

Here we can choose the name and path where we want to deploy the jar file and also include/exclude specific libraries packaged with the jar. This is important because by default IntelliJ will include the two SFS2X libraries we’ve added as part of the final jar.

We don’t want this, because those libraries are already part of the SFS2X runtime and we just use them as dependencies but we don’t need them included in our final jar. Make sure to select and remove sfs2x.jar and sfs2x-core.jar from the list, apply the changes and finally we can hit the Build » Build artifacts option to deploy the code.

Finally we can open the SFS2X AdminTool, navigate to the Zone Configurator, add the Extension to our Zone of choice and voilà.

» Next steps and resources

To learn more about Kotlin and its capabilities we highly recommend several interesting resources:

In the next installment we will take a look at using Kotlin on the client side with SFS2X.

Enjoy!