Sending messages to the server
After looking at how to send messages from the server to the connected clients, let's swap sides and check how to perform the same operation, but in the opposite direction. We'll illustrate this scenario by building a simple JavaScript client that will send a message to the server as soon as the connection will be available.
This specific case is not really useful if taken alone, because any communication from client to server could be easily done by using plain old HTTP. However, when combined with the reception counterpart, it enables the construction of complex, real-time client-server workflows.
Getting ready
Before writing the code of this recipe, we need to create a new empty web application, which we'll call Recipe27
.
How to do it…
Let's first check how to build the server portion using the following steps:
We first add a new class named
EchoConnection
that is derived fromPersistentConnection
, as we already did in the previous recipes of this chapter, and...