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!