scala-mqtt-client


License

License

MIT
Categories

Categories

Scala Languages CLI User Interface
GroupId

GroupId

com.github.hyjay
ArtifactId

ArtifactId

scala-mqtt-client_2.12
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

scala-mqtt-client
scala-mqtt-client
Project Organization

Project Organization

com.github.hyjay

Download scala-mqtt-client_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.hyjay/scala-mqtt-client_2.12/ -->
<dependency>
    <groupId>com.github.hyjay</groupId>
    <artifactId>scala-mqtt-client_2.12</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.hyjay/scala-mqtt-client_2.12/
implementation 'com.github.hyjay:scala-mqtt-client_2.12:0.1.0'
// https://jarcasting.com/artifacts/com.github.hyjay/scala-mqtt-client_2.12/
implementation ("com.github.hyjay:scala-mqtt-client_2.12:0.1.0")
'com.github.hyjay:scala-mqtt-client_2.12:jar:0.1.0'
<dependency org="com.github.hyjay" name="scala-mqtt-client_2.12" rev="0.1.0">
  <artifact name="scala-mqtt-client_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.hyjay', module='scala-mqtt-client_2.12', version='0.1.0')
)
libraryDependencies += "com.github.hyjay" % "scala-mqtt-client_2.12" % "0.1.0"
[com.github.hyjay/scala-mqtt-client_2.12 "0.1.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.8
io.netty : netty-all jar 4.1.33.Final
co.fs2 : fs2-io_2.12 jar 1.0.3
org.slf4j : slf4j-log4j12 jar 1.7.26

test (2)

Group / Artifact Type Version
org.scalatest : scalatest_2.12 jar 3.0.5
io.moquette : moquette-broker jar 0.12.1

Project Modules

There are no modules declared in this project.

scala-mqtt-client

Build Status

An asynchronous, reliable and seamless MQTT client in Scala, based on Netty.

Getting Started

MQTT client

With the library you can create a MQTT client, send and receive any MQTT packets as simple as it can be.

import com.github.hyjay.mqtt.core._
import com.github.hyjay.mqtt.netty.NettyMqttClient

val client  = new NettyMqttClient("localhost", 1883, tls = false)
// Connect, subscribe a topic and publish a message to the topic
val pingpong = for {
  connack <- client.connect(CONNECT("CLIENT_ID"))
  _ = client.send(SUBSCRIBE(Seq(("TOPIC", 0))))
  suback <- client.pull()   
  _ = client.send(PUBLISH("TOPIC", "hello, world!".getBytes.toSeq))
  pub <- client.pull()
} yield pub

val receivedPub = Await.result(pingpong, 3.seconds)
println(s"Got self-published message $receivedPub")

MQTT actor

Also the library provides a higher abstracted API than MQTT client, MQTT actor. MQTT actor helps developers focus on business logic with MQTT but not the MQTT protocol specification. The library handles any work for satisfying the MQTT specification behind the scenes, for instance sending PINGREQ periodically.

With the library you can write business logic with MQTT as simple as it can be.

// A program that publishes a message every minute.
import java.util.concurrent.Executors
import com.github.hyjay.mqtt.core._
import com.github.hyjay.mqtt.actor._

val actor = new MqttActor {

  import scala.concurrent.ExecutionContext.Implicits.global

  private val scheduler = Scheduler()(Executors.newScheduledThreadPool(1))
  
  private def executeEveryMinute(run: () => Unit): Future[Unit] = {
     run()
     scheduler.sleep(1.minute).flatMap(executeEveryMinute(run))
  }
  
  override def onReceived(packet: Packet, packetSender: MqttPacketSender): Unit = packet match {
    case CONNACK(0, _) => 
      // Successfully connected. Starting publishing
      val publishMessage = PUBLISH("TOPIC", "hello, world".getBytes.toSeq)
      executeEveryMinute(() => packetSender.send(publishMessage))
    case _ =>
      // Ignore other messages
  }
}

val connectionConfig = ConnectionConfig("localhost", 1883, tls = false, CONNECT("CLIENT_ID", keepAlive = 30.seconds))

// Run the actor. Return Future[Unit] that ends when the MQTT connection gets disconnected
MqttActor.run(connectionConfig, actor)

Dependencies

Add this to your build.sbt:

libraryDependencies += "com.github.hyjay" %% "scala-mqtt-client" % Version

Testing

sbt test

License

MIT - See LICENSE for more information.

Versions

Version
0.1.0