3

I have an application using Spring Framework / Spring Boot / Spring Messaging/Websockets and am going to be deploying it to Elastic Beanstalk. You can think of the application as a chat application (it actually does have chat features)

Scenario

Here is an example scenario:

Client A <-> Server A Client B <-> Server B Client C <-> Server B 

Now, if Client A posts a message, using spring messaging, if I send that message to all connected clients, only Client A will see it because only Client A is connected to Server A, and likewise if Client B does, only Clients B and C will see it, not Client A.

So this leaves me with a problem of what options I have.

Possible Solutions

If possible, I would like to use an Amazon service as I am already in their cloud platform.

I thought about using Amazon SQS, having each server subscribe to the same queue, and then sending all notifications through it, but I believe all requests with SQS are active, so I would have to do polling, and would create a significant delay.

Does anyone know of a good solution for this problem? I can set up a server to handle all web-sockets, but that is not optimal.

Thanks in advance!

7
  • I am also considering using something like Firebase, as long as the latency is not bad. Commented Jan 30, 2015 at 20:32
  • 1
    Did you want Server A and Server B running on the same instance? Or do you want them running completely separate from each other? Commented Jan 30, 2015 at 21:11
  • They are autoscaling, so they are separate servers created when the load is high, and automatically removed when the load is low. Commented Jan 31, 2015 at 2:11
  • 1
    does long polling make sense? docs.aws.amazon.com/AWSSimpleQueueService/latest/… Commented Feb 1, 2015 at 14:06
  • 1
    Why not use Dynamo and integrate with SNS when Dynamo receives a new message on a specific channel? Commented Feb 2, 2015 at 22:04

1 Answer 1

2

I am successfully doing almost exactly what you're describing.

Though I am not using spring boot, I am using spring and web sockets to send push message to browser clients when running in a cluster via elastic beanstalk.

The way I dealt with the cluster is by building a sub system where by I can send messages to other nodes.

So when I want to send a web socket message from node A I send the web socket message from that node (it will send to all it's registered clients) and then send a message to the other nodes for them to send to all their clients.

The "intra cluster" messaging is done with SNS + SQS. On startup each node creates a queue (identified by it's instanceid), registers that queue on a topic (all nodes use the same topic) and then starts a listener for new messages on that queue. This listener uses long polling. Amazon added support for "long polling" on it's queues a while back, it wasn't available at first. What it implies is that clients can block for up to 20 seconds waiting for a message, as soon as a message is received, that message will be processed - so your latency is very low. When I want to send a message a post to the topic and the message gets routed to all the queues registered on that topic. I also then built filtering in the listener to allow a message to go only to "other" clients (i.e. don't send this to myself option).

I see you did mention RabbitMQ... The weakness of my approach is that it does not support the richness that is possible if you use a message broker that supports the STOMP message protocol. Doing this means you don't have to worry about clustering at all, you just send to the broker and it handles the heavy lifting. I guess for me that would be my next step if I ever need a richer solution.

If someone could build a STOMP client backed by SQS that would be a boon for doing web socket message on spring in AWS.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for taking the time to write up an answer! We did end up using RabbitMQ and got it working great :) Will have to write up what we did some time
I'm just keen to know... did you use a dedicated ec2 instance for rabbit mq and if so did you cluster it? for fail over etc
It was dedicated unfortunately, we also had our MongoDB server on it and given the current size of userbase it could grow a lot before we need to worry about clustering it
Wow... adding rabbitmq to the mix was a lot easier than I expected. Spring Boot FTW

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.