S
S
Sergey2015-12-23 14:19:19
JavaScript
Sergey, 2015-12-23 14:19:19

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

15 answer(s)
R
Rafis Bagautdinov, 2015-12-24
@bagau

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

M
Max Kamashev, 2015-12-31
@ukko

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.

W
winbackgo, 2015-12-23
@winbackgo

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.

V
Viktor, 2015-12-23
@Levhav

Just the other day I wrote an article with a list of existing comet servers.

S
Sergey Sokolov, 2015-12-31
@sergiks

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.

S
SilverSlice, 2015-12-24
@SilverSlice

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.

S
Sergey, 2015-01-01
@frost18

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.

V
Vanes Ri_Lax, 2015-12-31
@vanesxl

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.

D
Dmitry Evgrafovich, 2015-12-31
@Tantacula

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) .

T
timfcsm, 2015-12-23
@timfcsm

elephant.io

A
Aizharik, 2015-12-23
@aizhar777

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

B
Ba3reH, 2015-12-23
@Ba3reH

You can look in the direction of the WAMP protocol wamp-proto.org
The site has implementations in different languages, including PHP and JavaScript.

D
dizews, 2015-12-28
@dizews

here is my Yii2 solution. there is a bunch of php with nginx module.
If you have any questions, write suggestions or pull requests :)

L
LAV45, 2015-12-31
@LAV45

An example of using WebSockets (Ratchet) & YiiFramework 2.
https://vk.com/yiiframework?w=wall-10814290_8238%2Fall
gostash.it/ru/stashes/346

A
Andrey Vorobyov, 2016-01-04
@Santacruz

www.websocket.org/echo.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question