A Java implementation of socket.io-emitter
This project uses jackson-dataformat-msgpack and RedisTemplate or RedisPool.
Gradle:
dependencies {
implementation 'io.github.deppan:socket.io-java-emitter:1.0.5'
}Maven:
<dependency>
<groupId>io.github.deppan</groupId>
<artifactId>io-java-emitter</artifactId>
<version>1.0.5</version>
</dependency>RedisClient redisClient = new RedisClient(redisTemplate);
Emitter io = new Emitter(redisClient);
io.emit("event","Hello World!");RedisClient redisClient = new RedisClient(redisPool);
Emitter io = new Emitter(redisClient);
io.emit("event","Hello World!");client is a wrapper that wraps the redis instance and publishes the message.
The following options are allowed:
key: the name of the key to pub/sub events on as prefix (socket.io)parser: parser to use for encoding messages to Redis (jackson-dataformat-msgpack)
nsp: namespace, default is "/"
Specifies a specific room that you want to emit to.
Specifies a specific room that you want to exclude from broadcasting.
Specifies a specific namespace that you want to emit to.
Makes the matching socket instances join the specified rooms:
// make all Socket instances join the "room1" room
io.socketsJoin("room1");
// make all Socket instances of the "admin" namespace in the "room1" room join the "room2" room
io.of("/admin").in("room1").socketsJoin("room2");Makes the matching socket instances leave the specified rooms:
// make all Socket instances leave the "room1" room
io.socketsLeave("room1");
// make all Socket instances of the "admin" namespace in the "room1" room leave the "room2" room
io.of("/admin").in("room1").socketsLeave("room2");Makes the matching socket instances disconnect:
// make all Socket instances disconnect
io.disconnectSockets();
// make all Socket instances of the "admin" namespace in the "room1" room disconnect
io.of("/admin").in("room1").disconnectSockets();
// this also works with a single socket ID
io.of("/admin").in(theSocketId).disconnectSockets();Send a packet to the Socket.IO servers in the cluster.
// Send the event and message to all servers.
io.serverSideEmit("forward", Map.of("hello", "world"));