E
E
Evgeniy Odinets2016-09-02 19:35:04
Redis
Evgeniy Odinets, 2016-09-02 19:35:04

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 рассылает сообщение клиентам.


Friend request example:
Клиент шлет ajax API -> api пишет в базу -> api отправляет событие на nodejs через redis ->
 nodejs рассылает клиенту.


Or maybe the client should send through the socket, and that, in turn, on the api?

How to implement this scheme? How should it ideally work and who should be responsible for what? Should Node look into the base? Or communicate with the database through API? Help advice please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Philipp, 2016-09-02
@evgeniy2194

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.

K
Konstantin B., 2018-03-04
@Kostik_1993

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 question

Ask a Question

731 491 924 answers to any question