K
K
Konstantin Frank2016-04-26 15:16:34
JavaScript
Konstantin Frank, 2016-04-26 15:16:34

How to update a page using WebSockets?

Hello!
I'm learning to develop with React.JS and it's time to deal with websockets. There is, for example, a page with a list of links. Also on this page there is a form for adding links. When adding a link by any user, it is required to update the list of links in real time. The task is pretty standard, but there is very little material to study. I will clarify that the data is stored in the MySQL database on one server, and the front-end is located on another.
Now I'll tell you a little about what I want to do. As I understand it, you just need to use pure Javascript to connect to the WebSockets server ( https://learn.javascript.ru/websockets).When you add a link, it will appear in the MySQL database and after that you can simply send socket.send("refresh"). After that, the server will send messages with new data from the database and the onmessage method will pick up. But the issue remains with the WebSockets server itself. I would not want to delve into the study of it, therefore I was looking for ready-made solutions. One of the search results was a repository - https://github.com/nicokaiser/php-websocket. Does it make sense to use this solution? If I understand correctly, then for it to work, it is enough to create a file like https://github.com/nicokaiser/php-websocket/blob/m...and change the onData method to handle refresh. But in this case, I do not understand the data distribution mechanism. Suppose, with the refresh (onData) method, the server will send a new entry to everyone using $this->_sendAll($encodedData), and with clientConnected, it will send out the entries that were added before the connection. Am I thinking right?
I would be grateful for any advice!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question