Answer the question
In order to leave comments, you need to log in
How to start using WebSocket technology?
In a project I'm doing in Yii2, there are toast notifications for asynchrony, I bombarded the server with ajax requests every 10 seconds. Now I need to do an online consultation, and then also a messenger. I am studying technologies that will help me in this, I refused "long polling" in favor of "WebSocket".
I want to try websockets right away, I don’t know where to start. The search suggests individual servers, frameworks, etc. (phpdaemon, ratchet, libevent), can you try something simpler, some examples in pure php and js, or a simple library to work with?
And in general, I think correctly, is web sockets the right solution for chat?
Answer the question
In order to leave comments, you need to log in
At my last job, I wrote an online consultant using web sockets. On php server with websocket, on javascript client. I commented the code, you can see it here on Github .
The websocket.php file is the logic of the websocket itself, with comments. I didn't understand anything about websockets, so I commented every line to understand.
file start_socket.php - work with websocket.
Written without the use of frameworks.
The online consultant is basically working, but I couldn’t finish it, I quit my job
You should read about daemonizing processes, how to restart them in case of crashes, how to handle various situations, clear memory, and more.
In this daemon, include any popular library (ratchet, wrench, ...).
Ps I don't see the point in changing technologies to nodejs. Nodejs is not an ideal technology with its own problems.
I don’t know how ratchet is there, but I didn’t manage to raise a sane socket server on php. Switched to javascript (node.js). Satisfied. I use socket.io and socketcluster.io. There is also lightstreamer (java), which is powerful stuff, all out of the box, but the free version has a number of limitations.
Just the other day I wrote an article with a list of existing comet servers.
Raise the chat / consultation on the nodejs server + socket.io - there are ready-made examples. For chat and any real-time interaction, websockets is a good choice.
The easiest way to get started is with the official Ratchet guide. You can also watch the video , in which the same thing is clearly shown.
Thank you all, very helpful.
I used Ratchet, I did everything I wanted, I was satisfied, for the first acquaintance and understanding of the work of the comet server, that's it.
Well, as for the client part, everything is simple here, you can see it here
. But I wouldn’t do the server part in php ... maybe, of course, I have peculiar views on this language, but forcing the script to run in a constant cycle is some kind of nonsense .. I would advise writing a server in a language like java, there are very good libraries, everything is simple.
In the time that has passed since writing this question, you have most likely chosen a websocket server for yourself, but if not, take a closer look at centrifugo. habrahabr.ru/company/mailru/blog/266017 - article about the server, https://fzambia.gitbooks.io/centrifugal/content/li... - interaction with php (there is a package for Laravel, there may also be one for yii) .
Also in my project, when implementing a chat between users, I started using long polling but then abandoned in favor of Ratchet and push socketo.me/docs/push
You can look in the direction of the WAMP protocol wamp-proto.org
The site has implementations in different languages, including PHP and JavaScript.
here is my Yii2 solution. there is a bunch of php with nginx module.
If you have any questions, write suggestions or pull requests :)
An example of using WebSockets (Ratchet) & YiiFramework 2.
https://vk.com/yiiframework?w=wall-10814290_8238%2Fall
gostash.it/ru/stashes/346
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question