📜 ⬆️ ⬇️

Simple real-time communication with the visitor

I have long been interested in the web in real time. Today there are already a number of libraries for this. In this topic, I want to tell you about the easiest way we recently tested, using the external service Pusher.com.

If today asynchronously sending a message to the server is easier than ever, then with reverse transport, not everything is so rosy. Service just undertakes this task.


Everything works very simple. A pusher javascript is inserted into the page, it opens a connection to the pusher servers and subscribes to messages via a channel. When you want to send a message, you call the pusher API and send the message to the channel. All users who subscribe to the channel will receive it.
Thus, it is very easy to organize a mass mailing of messages to all necessary users online. That makes it very easy to create a chat - just insert a pusher on the page, the script on your server will receive messages from the POST, “pull” the API Pusher`a, and that will send them to all clients via the web sockets. Similarly, you can do more complex things, for example, collaborative graphic editors - everything is in the examples and documentation .

From myself, I note that several months ago we transferred chat and online to pusher, thereby solving several problems at once:
- significantly accelerated message delivery
- reduced user traffic
- reduced the load on our servers - now we only receive messages, and pusher deals with sending out to users
- improved system scalability
')
When moving, there were some doubts - whether users will face problems. How well different browsers will be supported. The fears were in vain - Pusher uses WebSockets, and when they are unavailable (for example, in ancient non-UE browsers), the good old emulation via Flash Sockets is exactly what I used in my model chat on the web socket . Practice has shown that this is enough. To complicate and degrade to Long Polling, Multi Part and others is not required.

We recently decided to use it in another more complex project.
If interested, then how to implement, I will describe in detail.

Summarize

Pros:
- very easy to connect to the site - there are ready-made PHP and Python
- if there is no ready-made library for your language, then you can use a simple API, for that, curl`a is enough
- removes scaling problem - pusher is hosted on Amazon servers

Minuses:
- paid

Source: https://habr.com/ru/post/130530/


All Articles