Reading — step 1 of 3
The ECHO Command
~1 min readRESP Protocol Fundamentals
ECHO — Returning Data
Now that PING works, let's add ECHO — a command that returns whatever you send it.
How ECHO Works
ECHO <message> returns the message as a RESP bulk string:
ECHO hello → $5\r\nhello\r\n
ECHO foobar → $6\r\nfoobar\r\n
Bulk String Encoding
A bulk string has the format: $<length>\r\n<data>\r\n
The length is the number of bytes in the data (important for binary safety — though for ASCII text, bytes = characters).
Examples:
""(empty) →$0\r\n\r\n"hi"→$2\r\nhi\r\n"hello world"→$11\r\nhello world\r\n
Building a RESP Encoder
You should start building helper functions:
encode_bulk_string(s) → "$<len>\r\n<s>\r\n"
encode_simple_string(s) → "+<s>\r\n"
These will be reused in every future lesson. Write them once, use them everywhere.
Your Task
Add ECHO to your program alongside PING. Both commands must work in the same session.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…