I
I
Ivan Vorobei2016-08-05 00:11:58
API
Ivan Vorobei, 2016-08-05 00:11:58

Add messenger to rest api?

Implemented Rest API on Laravel (generally on Lumen, but one oven) for the project. Everything is in the best traditions, get / post requests, json gives. Nothing complicated.
There was a need to add a messenger, and not just send a message, but to see when a message is typed, and when it is viewed, and when it is delivered.
After reading articles on Habré, I came to the conclusion that I need web sockets. Am I right?
If yes, then I have a bad idea of ​​how web sockets and rest api get along. It looks like normal requests for the rest, only the connection is kept (and no additional information needs to be passed)
The question is vague and related to a misunderstanding of the concept of web sockets, everyone is talking about "merits" and "usages". I would like to present how it works and is used in practice.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
werevolff, 2016-08-05
@ivanvorobei

I don’t know if it will help or not, but I would start the search with ready-made solutions: For
the backend
For the frontend Ratchet
is used as a library that implements web sockets It turns out that we will have two types of messages: 1. Status of the list of users. 2. Messages from users.
If we were talking about angular, then we could track the focus on the input, or changes in the model attached to the input. Or, it would be possible to complicate the process and study the number of model changes per unit of time. There is still an opportunity to catch keypress. In general, when we set what the current client is printing, we are sending a single type of message to the server. If the user submits a different type of message.
This is how we can use the Redis/RabbitMQ server to save the user statuses of open conversations (and remove the instance if the conversation is closed). The following scheme is obtained:
RAM stores {'dialogIDqweqweqwe': {'users': {'vasya': {'state': 'online', 'activity': 'printing'}, 'petya': {'state': ' online', 'activity':
When Petya changes status, his client sends {'action': 'userActivity', 'value': 'printing'}. As far as I understood the dock from the ratchet, when a message arrives from one of the clients, the onMessage hook fires. In it, we check the action of the response and either save the data and do $client->send (if something has changed compared to the previous state), or ignore the changes.
You understand the mechanism of web sockets correctly. On the backend rises server - an application that implements the WebSocket protocol . The general idea of ​​the work gives its name. Server is a socket to which N clients connect and listen for changes (wait for events).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question