Return to site

🔌🧩 WEBSOCKET HANDLERS: WHICH MESSAGE TYPES ARE VALID?

· java

🔸 TLDR

In the Java WebSocket API (JSR 356 / Jakarta WebSocket), you can attach message handlers only for TEXT, BINARY, and PONG. ✅

PING and ERROR are not message types you register a MessageHandler for. 🚫

Section image

🔸 TRY THIS QUIZ (BEFORE READING THE ANSWER) 🧠

Question (rephrased):

In a WebSocket session, for which incoming message types are you allowed to register a MessageHandler?

▪️ Error Message

▪️ Binary Message

▪️ Pong Message

▪️ Ping Message

(Select 2)

🔸 ANSWER ✅

Correct choices:

▪️ Binary Message

▪️ Pong Message

Simple explanation:

In this API, handlers are designed for the “payload” message types you receive from the client/server:

▪️ TEXT (not listed in the options above, but it’s valid)

▪️ BINARY ✅

▪️ PONG ✅

And there’s also an important rule:

👉 You can register at most one handler per message type per session (one for text, one for binary, one for pong). 🔒

🔸 WHY “PING” IS NOT IN THE LIST 🏓

PING is a control frame. In practice, you typically send pings (or let the container handle heartbeats) rather than registering a standard MessageHandler for “Ping messages”.

🔸 TAKEAWAYS 🎯

▪️ Register handlers for: TEXT, BINARY, PONG ✅

▪️ One handler per type, per session — no duplicates 🔐

▪️ Pings are usually for keep-alive / control, not app payload 🏓

#Java #WebSocket #JakartaEE #JavaEE #BackendDevelopment #Networking #APIs #SoftwareEngineering #Certification #TechSkills

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaBook👇