AWS load platform and socket.io

I have a socket.io socket room, whose traffic is becoming more and more as we work on the same machine. We ran tests using the ws library for sockets, and they perform much better, which uses our equipment better. It will be worth it to rewrite our application.

The socket.io application allows users to create private chats that are implemented using namespaces. For example,

localhost:8080/room/1
localhost:8080/room/2
localhost:8080/room/3

When everything happens in one instance, it is quite simple, but now we want to expand this capacity to several nodes.

We run this instance in the Amazon cloud. Previously, it looked like scaling websockets was a problem with ELB. We noticed that Amazon now also supports application load balancing, which supports websockets. That sounds great, but after reading the documentation, I have to admit that I really don't know what that means. If I use socket.io with thousands of namespaces, I just put instances behind this ALB and everything will work? My main questions are:

If x is the number of users joining the namespace, will ALB automatically redirect my messages to the right users as well? So, let's say I have 5 instances of vanilla socket.io working for ALB. User 1 creates a namespace. After a few hours, the transition will pass and user 99999 will come and want to join this namespace, should there be any additional code written for this, or should alb redirect everything where it should go? The same goes for sending and receiving messages?

+6
source share
1 answer

ALB , , , , .

socket.io :

, Socket.IO, , if ( ) - .

, , , . socket.io-adapter ( ), , Redis: socket.io-redis:

var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));

ALB

ALB, hands.Socket.io , websocket, , , , , , .

enter image description here enter image description here


, ALB Routing socket.io.

redis. , , userA 4, , , ? ?

, EC2. , ALB Routing

N > 1 .

1:

URL - :

/i1/room/550
/i1/room/20
/i2/room/5
/i5/room/492

:

/{instance-number}/room/{room-id}

, ALB .

2:

N (N - , )

Picture

3:

> X > "" > "" > " X >

Target group X > EC2 Instance X
Target group Y > EC2 Instance Y

enter image description here

4:

ALB

> ALB > > /

enter image description here

5:

/ :

  • IF > : /iX/room/*
  • THEN > : instanceX

enter image description here


enter image description here

:

  • /i1/room/550 EC2 1.
  • /i2/room/200 EC2 2

..

, . , .

, .

+9

Source: https://habr.com/ru/post/1016893/


All Articles