broadcast
Introduction
Broadcast is one of the communication methods, also known as push. It is commonly used to proactively send data to one user, multiple users, or all users on the server. For example:
- Single user: send rewards to a specified online user.
- Multiple users: broadcast data to users in the same room. For example, when one user shoots a bullet, broadcast bullet data to other users in the room.
- Full server: broadcast messages to all online users on the server, such as announcements or maintenance notices.
Request parameters in the communication model support int, long, bool, String, Object, List<Integer>, List<Long>, List<Boolean>, List<String>, List<YourClass>
Example Source Code
see https://github.com/iohao/ionet-examples
path : ionet-cookbook-code
- BroadcastAction
Broadcast to a Specified User
Broadcast to a specified single user. API names start with broadcastUser.
@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastUser)
private void triggerBroadcastUser(long userId) {
var communication = CommunicationKit.getCommunication();
// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
communication.broadcastUser(userId, BroadcastCmd.broadcastUserObject, dataObject);
}
}
Broadcast to Specified Multiple Users
Broadcast to specified multiple users. API names start with broadcastUsers.
@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastUsers)
private void triggerBroadcastUsers(List<Long> userIdList) {
var communication = CommunicationKit.getCommunication();
// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
communication.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersObject, dataObject);
}
}
Broadcast to Everyone
Broadcast to everyone. API names start with broadcastMulticast.
@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastMulticast)
private void triggerBroadcastMulticast() {
var communication = CommunicationKit.getCommunication();
// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
communication.broadcastMulticast(BroadcastCmd.broadcastMulticastObject, dataObject);
}
}
Broadcast Routing Suggestion
Use an incrementing broadcast start marker to specify broadcast routes, so you do not need to care about specific route values.
public interface BroadcastCmd {
int cmd = CmdModule.broadcastCmd;
// ---------- broadcastUser ----------
int triggerBroadcastUser = 1;
...
AtomicInteger inc = new AtomicInteger(10);
private static CmdInfo ofBroadcastCmd() {
return CmdInfo.of(cmd, inc.getAndIncrement());
}
// ---------- broadcastUser ----------
CmdInfo broadcastUserEmpty = ofBroadcastCmd();
CmdInfo broadcastUserInt = ofBroadcastCmd();
CmdInfo broadcastUserBool = ofBroadcastCmd();
CmdInfo broadcastUserLong = ofBroadcastCmd();
}
// test
@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastUser)
private void triggerBroadcastUser(long userId) {
var communication = CommunicationKit.getCommunication();
// ---------- empty ----------
communication.broadcastUser(userId, BroadcastCmd.broadcastUserEmpty);
// ---------- int ----------
int dataInt = inc.getAndIncrement();
communication.broadcastUser(userId, BroadcastCmd.broadcastUserInt, dataInt);
// ---------- boolean ----------
boolean dataBool = inc.getAndIncrement() % 2 == 0;
communication.broadcastUser(userId, BroadcastCmd.broadcastUserBool, dataBool);
}
}
More Examples
For more usage examples, see Examples Communication Broadcast.
Enable Broadcast Logging
This is an enterprise feature
After enabling broadcast logs, you can locate broadcast code positions.
@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastUser)
private void triggerBroadcastUser(long userId) {
// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
communication.broadcastUser(userId, BroadcastCmd.broadcastUserObject, dataObject);
// ---------- string ----------
String dataString = "ionet-" + inc.getAndIncrement();
communication.broadcastUser(userId, BroadcastCmd.broadcastUserString, dataString);
}
}
Click BroadcastAction.java:45 in the console output to jump to the corresponding broadcast business code.
- CN
- EN
┏━━━━━ 广播. [(BroadcastAction.java:11)] ━━━ [cmd:7-15 458767]
┣ userId: 1378604058
┣ 数据: BookMessage(bookName=book-19, authorName=ionet)
┣ 时间: 2025-10-29 13:07:25
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
┏━━━━━ 广播. [(BroadcastAction.java:15)] ━━━ [cmd:7-14 458766]
┣ userId: 1378604058
┣ 数据: ionet-18
┣ 时间: 2025-10-29 13:07:25
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
┏━━━━━ Broadcast. [(BroadcastAction.java:11)] ━━━ [cmd:7-15 458767]
┣ userId: 1378604058
┣ Data: BookMessage(bookName=book-19, authorName=ionet)
┣ Time: 2025-10-29 13:03:46
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
┏━━━━━ Broadcast. [(BroadcastAction.java:15)] ━━━ [cmd:7-14 458766]
┣ userId: 1378604058
┣ Data: ionet-18
┣ Time: 2025-10-29 13:03:46
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━