PHP implements real-time push at the frontend and backend based on websocket

Source: Internet
Author: User
Tags php websocket
Register an account on the goeasy website and create an app. after an APP is created, the system automatically generates two keys for the app. one key can be used for receiving and pushing, and the other key can only be used for receiving (subscriberkey ). PHP websocket real-time message push
The implementation steps are as follows:
1. get GoEasy appkey.
Register an account on the goeasy website and create an app. after an APP is created, the system automatically generates two keys for the app. one key can be used for receiving and pushing, and the other key can only be used for receiving (subscriber key ).
2. the client subscribes to a channel.
A. introduce goeasy. js on the client. js address: cdn. goeasy. io/goeasy. js
You must note that you cannot download goeasy. js to your local machine, because goeasy provides different js. official website descriptions based on different browsers.
B. subscription code

Var goEasy = new GoEasy ({appkey: 'subscriber key'}); // Here I use the subscriber key, because I do not need to push any messages on my client, so there is no need to use supper key goEasy. subscribe ({channel: 'sdnnotification', onMessage: function (message) {alert ('essagereceived: '+ message. content); // receives the pushed message }});

3. foreground push and background push
3.1. push to the same channel at the front end
On the page where goeasy. js is introduced, you can directly call goEasy. publish ('csdnnotification', 'Your friend 222 is online'). its usage is similar to subscription.
3.2. push to the same channel in the background. implement it using the restful API provided by GoEasy
API url: goeasy. io/goeasy/publish
Method: Post
Parameter list:

Channel: 'sdnnotification' // it must be consistent with the subscribed channel. Otherwise, the client will not receive the message. Therefore, we can also control which clients can receive messages based on the channel. Appkey: 'Er er key' // supper key content: 'blabla... must be used here ..... '// The content to be pushed

I hope to help you.

The above is the detailed content of PHP real-time push on the frontend and backend based on websocket. For more information, see other related articles in the first PHP community!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.