First define a Deal type in schema. graphQL:
We simulate a scenario where we expect to receive notification from the server side whenever a new transaction occurs.
Create a new subscription with a field of deal and type of the deal just defined:
This subscription implementation simply prints a notification every 2 seconds:
const deal = {
subscribe: (parent, args, { pubsub }) => {
const channel = Math.random().toString(36).substring(2, 15) // random channel name
setInterval(
() => pubsub.publish(channel, { deal: {description: "New deal!"}}),2000);
return pubsub.asyncIterator(channel)
},
}
module.exports = {
deal,
}
Copy the code
Next, you’ll see a New Deal notification every 2 seconds:
For more of Jerry’s original articles, please follow the public account “Wang Zixi “: