Answer the question
In order to leave comments, you need to log in
SocketIO + Laravel + Redis + MySQL. how to be?
Good day.
Now we have:
Laravel - API
Node + Socket.io - ws
Redis - a layer between api and ws for sending push notifications + cache, etc.
I am writing something similar to a messenger, but with its own twist.
How to correctly implement communication Client -> ws -> api -> mysql.
Let's take messages as an example. Now we have:
Клиент отправляет сообщение Node -> Node отправляет сообщение API -> api пишет в базу
|
Node рассылает сообщение клиентам.
Клиент шлет ajax API -> api пишет в базу -> api отправляет событие на nodejs через redis ->
nodejs рассылает клиенту.
Answer the question
In order to leave comments, you need to log in
In my opinion, here Laravel is sewn on the side with a hint. Node.js knows how to work with MySQL.
I would implement a similar service as follows:
Node.js as the main server with API and data transport via Socket.io.
Redis as a message bus between Node.js server instances.
Web sockets allow data exchange and file transfer. You don't really need Ajax here.
IMHO, sending a message should look like this:
Node.js accepts a message from sockets, issues an identifier to the message, and then passes it on the message bus (Redis). There are several listeners on the bus, one of which is responsible for storing data in the database, while others send messages to clients.
A friend request looks like this:
Node.js receives a message from web sockets, saves it to the database, sends a request over the message bus to the recipient.
When the application is loaded, the client connects to the server via web sockets, then registers on the message bus.
I want to add a couple of points that you need to consider when developing an application.
1. You can have multiple application instances running on the same server or on different ones.
2. It is necessary to control the moment when the system tries to resend the message in case of loss of connection with the instance - often happens with an unreliable connection to the network, especially in mobile networks.
If you have Laravel as an API, then Node should only be an intermediary between the client and the API, namely, its task is when the API calls an event and sends it to Redis on a specific channel. In turn, Node should listen for changes in the radish, and when such an event occurs, Node sends it via WebSocket to clients that are subscribed to the channel. That is, if you have an API on Laravel, then Node should not go into the database, but just listen to the radish.
Another thing is when you have Node as a server. Then you don’t need a radish here, the node does all the processing and it sends data to the channel on its own
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question