Quick Start

Setup Build Tools (Maven or Gradle)

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
    
<dependencies>
    <dependency>
        <groupId>com.github.Szczurowsky</groupId>
        <artifactId>RatOverNATS</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

Connect to NATS server

public class Main {

    public static void main(String[] args) {
        RatOverNats ratOverNats = new RatOverNatsBuilder()
                .uri("nats://localhost:4222")
                .build();
    }

}

Register message handler

PacketID in packet and message handler must be same to handle action

As you can see in constructor we've two values. First is packet id and second is channel name

TestMessageHandler.java
public class TestMessageHandler extends RatMessageHandler<String> {

    protected TestMessageHandler() {
        super(1, "test");
    }

    @Override
    protected void onReceive(String s, Message message) {
        System.out.println(s);
    }
}

Register handler

public class Main {

    public static void main(String[] args) {
        RatOverNats ratOverNats = new RatOverNatsBuilder()
                .uri("nats://localhost:4222")
                .registerHandler(new TestMessageHandler())
                .build();
    }

}

Publish message

Here we're setting packetId to 1 and channel to test with payload - string also test

ratOverNats.publish("test", new Packet<>("test").setPacketId(1));

Last updated