How to send data in real time in Java class library like SDK

We can use JavaScript client exchange data as follows:
socket.onchannelmessage = (channelMessage) => {
console.info(“Received chat message:”, channelMessage);
};

const channelId = “pineapple-pizza-lovers-room”;
var response = await socket.send({ channel_join: {
type: 1, // 1 = room, 2 = Direct Message, 3 = Group
target: channelId,
persistence: false,
hidden: false
} });
console.info(“Successfully joined channel:”, response.channel.id);

const messageAck = await socket.send({ channel_message_send: {
channel_id: response.channel.id,
content: {“message”: “Pineapple doesn’t belong on a pizza!”}
} });
console.info(“Successfully sent chat message:”, messageAck);

How to send data in real time in Java class library like this.

This is covered in the documentation on this page but for the sake of completion the equivalent Java code looks like this:

SocketClient socket = client.createWebSocket();

SocketListener listener = new AbstractSocketListener() {
  @Override
  public void onChannelMessage(final ChannelMessage message) {
    System.out.format("Received a message on channel %s", message.getChannelId());
    System.out.format("Message content: %s", message.getContent());
  }
};

socket.connect(session, listener).get();

final string roomName = "pineapple-pizza-lovers-room";
final Channel channel = socket.joinChat(roomName, ChannelType.ROOM).get();
System.out.format("Successfully joined channel: %s", roomName);

final String content = "{\"message\":\"Pineapple doesn’t belong on a pizza!\"}";
ChannelMessageAck sendAck = socket.writeChatMessage(channel.getId(), content);
System.out.format("Successfully sent chat message: %s", sendAck);
2 Likes

I used nakama-java-2.0.3-all.jar, but I didn’t find two methods, channeljoin and channelmessagesend

1 Like

Ah yes, I see the issue. I’ve fixed the code example above and the main documentation will be redeployed soon! Thanks for noticing it!

1 Like