Skip to content

deppan/socket.io-java-emitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

socket.io-java-emitter

A Java implementation of socket.io-emitter

This project uses jackson-dataformat-msgpack and RedisTemplate or RedisPool.

Download

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>

Usage

Using with RedisTemplate

RedisClient redisClient = new RedisClient(redisTemplate);
Emitter io = new Emitter(redisClient);
io.emit("event","Hello World!");

Using with JedisPool

RedisClient redisClient = new RedisClient(redisPool);
Emitter io = new Emitter(redisClient);
io.emit("event","Hello World!");

API

Emitter(client, opts, nsp)

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 "/"

Emitter#to(String... room):BroadcastOperator

Emitter#in(String... room):BroadcastOperator

Specifies a specific room that you want to emit to.

Emitter#except(String... room):BroadcastOperator

Specifies a specific room that you want to exclude from broadcasting.

Emitter#of(String namespace):Emitter

Specifies a specific namespace that you want to emit to.

Emitter#socketsJoin(String... room)

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");

Emitter#socketsLeave(String... room)

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");

Emitter#disconnectSockets(boolean close)

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();

Emitter#serverSideEmit(String ev, Object... args)

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"));

License

MIT

About

A Java implementation of socket.io-emitter

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages