ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - hypercube1024/firefly: Firefly is an asynchronous web framework for rapid development of high-performance web application.
Firefly is an asynchronous web framework for rapid development of high-performance web application. - hypercube1024/firefly
Visit Site

GitHub - hypercube1024/firefly: Firefly is an asynchronous web framework for rapid development of high-performance web application.

GitHub - hypercube1024/firefly: Firefly is an asynchronous web framework for rapid development of high-performance web application.

What is Firefly?

GitHub Workflow Status Maven Central License

Firefly framework is an asynchronous Java web framework. It helps you create a web application Easy and Quickly. It provides asynchronous HTTP, Websocket, TCP Server/Client, and many other useful components for developing web applications, protocol servers, etc. That means you can easy deploy your web without any other java web containers, in short, it's containerless. Using Kotlin coroutines, Firefly is truly asynchronous and highly scalable. It taps into the fullest potential of hardware. Use the power of non-blocking development without the callback nightmare.

Firefly core provides functionality for things like:

  • HTTP server and client
  • WebSocket server and client
  • HTTP, Socks proxy
  • HTTP Gateway
  • TCP server and client
  • UDP server and client

Event driven

The Firefly APIs are largely event-driven. It means that when things happen in Firefly that you are interested in, Firefly will call you by sending you events.

Some example events are:

  • some data has arrived on a socket
  • an HTTP server has received a request

Firefly handles a lot of concurrencies using just a small number of threads, so don't block Firefly thread, you must manage blocking call in the standalone thread pool.

With a conventional blocking API the calling thread might block when:

  • Thread.sleep()
  • Waiting on a Lock
  • Waiting on a mutex or monitor
  • Doing a long-lived database operation and waiting for a result
  • Call blocking I/O APIs

In all the above cases, when your thread is waiting for a result it can’t do anything else - it’s effectively useless.

It means that if you want a lot of concurrencies using blocking APIs, then you need a lot of threads to prevent your application grinding to a halt.

Threads have overhead regarding the memory they require (e.g. for their stack) and in context switching.

For the levels of concurrency required in many modern applications, a blocking approach just doesn’t scale.

Quick start

Add maven dependency in your pom.xml.

<dependencics>
    <dependency>
        <groupId>com.fireflysource</groupId>
        <artifactId>firefly-net</artifactId>
        <version>5.0.2</version>
    </dependency>

    <dependency>
        <groupId>com.fireflysource</groupId>
        <artifactId>firefly-slf4j</artifactId>
        <version>5.0.2</version>
    </dependency>
</dependencics>

Add log configuration file "firefly-log.xml" to the classpath.

<?xml version="1.0" encoding="UTF-8"?>
<loggers xmlns="http://www.fireflysource.com/loggers"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.fireflysource.com/loggers http://www.fireflysource.com/loggers.xsd">
    <logger>
        <name>firefly-system</name>
        <level>INFO</level>
        <path>logs</path>
    </logger>
</loggers>

HTTP server and client example:

fun main() {
    `$`.httpServer()
        .router().get("/").handler { ctx -> ctx.end("Hello http! ") }
        .listen("localhost", 8090)

    `$`.httpClient().get("http://localhost:8090/").submit()
        .thenAccept { response -> println(response.stringBody) }
}

WebSocket server and client example:

fun main() {
    `$`.httpServer().websocket("/websocket/hello")
        .onServerMessageAsync { frame, _ -> onMessage(frame) }
        .onAcceptAsync { connection -> sendMessage("Server", connection) }
        .listen("localhost", 8090)

    val url = "ws://localhost:8090"
    `$`.httpClient().websocket("$url/websocket/hello")
        .onClientMessageAsync { frame, _ -> onMessage(frame) }
        .connectAsync { connection -> sendMessage("Client", connection) }
}

private suspend fun sendMessage(data: String, connection: WebSocketConnection) = connection.useAwait {
    (1..10).forEach {
        connection.sendText("WebSocket ${data}. count: $it, time: ${Date()}")
        delay(1000)
    }
}

private fun onMessage(frame: Frame) {
    if (frame is TextFrame) {
        println(frame.payloadAsUTF8)
    }
}

TCP server and client example:

fun main() {
    `$`.tcpServer().onAcceptAsync { connection ->
        launch { writeLoop("Server", connection) }
        launch { readLoop(connection) }
    }.listen("localhost", 8090)

    `$`.tcpClient().connectAsync("localhost", 8090) { connection ->
        launch { writeLoop("Client", connection) }
        launch { readLoop(connection) }
    }
}

private suspend fun readLoop(connection: TcpConnection) = connection.useAwait {
    while (true) {
        try {
            val buffer = connection.read().await()
            println(BufferUtils.toString(buffer))
        } catch (e: Exception) {
            println("Connection closed.")
            break
        }
    }
}

private suspend fun writeLoop(data: String, connection: TcpConnection) = connection.useAwait {
    (1..10).forEach {
        connection.write(toBuffer("TCP ${data}. count: $it, time: ${Date()}"))
        delay(1000)
    }
}

Contact information

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory