This is a creation in Article, where the information may have evolved or changed.
Golang WebSocket Real-time message push
Here I record how to implement the server-side real-time communication with the client:
The implementation steps are as follows:
1. Get Goeasy Appkey.
Register an account on the Goeasy website and create a new app. After the app is created, the system automatically generates two keys for the app, one that can be used for both receiving and pushing (supper key), and the other to receive (Subscriber key).
2. The client subscribes to a channel.
A. Introducing Goeasy.js to the client, JS address: http://cdn.goeasy.io/goeasy.js
It is important to note that Goeasy.js cannot be downloaded locally, because Goeasy will provide different JS depending on the browser. The official web site also has a corresponding explanation.
B. Subscription code
var goeasy = new Goeasy ({appkey: ' subscriber key '});//I'm using subscriber key here, because I don't need to push any messages on my client, so there's no need to use supper key
Goeasy.Subscribe({Channel:' Csdnnotification ',OnMessage: function (message) {alert (' Meessage received: '+message.content);//received a push message} });
3. Push the background to the same channel. RESTful API implementations with Goeasy
API Url:http://goeasy.io/goeasy/publish
Mode: Post
Parameter list:
Channel:' Csdnnotification ' //The channel must be consistent with the subscription, otherwise the client will not receive the message. So we can also control which clients can receive according to the channel. Appkey:' supper key ' //must use supper key herecontent:' BlaBla ... ' //What to push
The
wants to be helpful to everyone.