Skip to content

oHate/Swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift

Release

What is Swift?

Swift is a Jedis Pub/Sub handler that makes it easier to broadcast messages (called payloads) to all subscribers.

Getting started

To get started with Swift, first add the JitPack repository. If you're using Maven, that looks like this:

<repository>  
    <id>jitpack.io</id>  
    <url>https://jitpack.io</url>  
</repository>

Next you will need to add Swift as a dependency. Replace LATEST with the latest GitHub release number.

<dependency>
    <groupId>com.github.stevensci</groupId>
    <artifactId>Swift</artifactId>
    <version>LATEST</version>
</dependency>

How do I use Swift?

First create an instance of Swift.

Swift swift = new Swift("networkName", "unitName", "redisUri");

networkName - The name of the network that payloads will be broadcasted on.

unitName - The name of the application or unit that broadcasted the payload, this field is used as the origin name in a payload.

redisUri - The uri that will be used to connect to Redis, for example redis://127.0.0.1:6379/0

Next you will need to create a Payload. We use Gson to serialize the Payload class as Json.

public class UserJoinPayload extends Payload {  
  
    private final String username;  
  
    public UserJoinPayload(String username) {  
        this.username = username;  
    }  
  
    public String getUsername() {  
        return username;  
    }  
      
}

In order to do anything useful when we receive the payload we need to created a listener. Payload listeners implement the PayloadListener interface, every payload handler is annotated with the @PayloadHandler annotation.

public class TestPayloadListener implements PayloadListener {  
  
    @PayloadHandler  
    public void onUserJoin(UserJoinPayload payload) {  
        System.out.println(payload.getUsername() + " has joined!");  
    }  
  
}

To receive and send these payloads you will need to register the two objects we created.

PayloadRegistry registry = swift.getRegistry();  
  
registry.registerPayloads(UserJoinPayload.class);  

registry.registerListener(new TestPayloadListener());

To send the payload you can broadcast it by using the broadcast method in Swift.

swift.broadcastPayload(new UserJoinPayload("John"));

Contributing

I love your contributions! Bug reports are always welcome! You can open a bug report on GitHub.

About

Redis pub/sub payload handler

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages