Introduction to remote adapters #440
Totodore
announced in
Announcements
Replies: 1 comment
-
The biggest release everyone was waiting for! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Introduction to remote adapters.
With socketioxide v0.16 you can now use remote adapters!
Remote adapters are a way for socket.io to communicate to other instances in a horizontally scaled environment. This is useful when you have multiple running instances of your application and you want to broadcast messages to all connected clients.
They work by exchanging messages between instances using a shared storage backend. This can be a database, a message queue, or any other shared storage that all instances can access. Currently only Redis pub/sub is supported, but more backends will be added in the future.
What can you do with remote adapters?
RemoteSocket
. It implements a subset of the socket API. You can use it to emit message (with ack or not), join/leave rooms, get the socket id, etc...What do you have to change in your code to use remote adapters?
The following structs are generic over the adapter you want to use, hence you need to, either specify the adapter type or generify your functions over the adapter type.
Prefer the latter as it allows you to switch between adapters without changing your code.
SocketIo<A: Adapter>
SocketIoLayer<A: Adapter>
SocketIoService<A: Adapter>
SocketRef<A: Adapter>
RemoteSocket<A: Adapter>
Socket<A: Adapter>
AckSender<A: Adapter>
You should use generic functions instead of closures for event handlers. This allows you to be generic over the adapter you want to use.
To use remote adapters, you need to create an instance of the adapter and pass it to the
Socketio
struct when creating a new server. Here's an example using the Redis adapter:Beta Was this translation helpful? Give feedback.
All reactions