Real Serverless Graphql Experience with Realtime Powers!
Real Serverless Graphql Experience with Realtime Powers!
npm i -g @kraken.js/cli
kraken new [--name|--version]
cd {name}
yarn dev
offline: Starting Offline: offline/us-east-1.
offline: Offline [http for lambda] listening on http://localhost:4002
offline: route '$connect'
offline: route '$disconnect'
offline: route '$default'
offline: Offline [websocket] listening on ws://localhost:4001
offline: Offline [http for websocket] listening on http://localhost:4001
Check the demo project
The websocket protocol implementation is based on Apollo subscriptions-transport-ws
With this simple schema and an even simpler resolver
type Subscription {
onPing(channel: String): Ping @sub(triggerName: "onPing#{channel}")
}
type Mutation {
ping(channel: String!): Ping @pub(triggerNames: ["onPing#{channel}"])
}
type Ping {
channel: String!
timestamp: Float!
}
{
Mutation: {
ping: (_, args) => ({
...args,
timestamp: Date.now()
})
}
}
...and you are ready to use pubsub on serverless
wscat --connect ws://localhost:4001
# initialize the connection (required)
> {"type":"connection_init"}
< {"type":"connection_ack"}
# start a subscription
> {"type":"start", "id": "1", "payload":{"query":"subscription { onPing(channel: \"global\") { channel timestamp }}"}}
# publish to the subscription
> {"type":"start", "id": "2", "payload":{"query":"mutation { ping(channel: \"global\") { channel timestamp } }"}}
# subscription response
< {"id":"1","type":"data","payload":{"data":{"onPing":{"channel":"global","timestamp":1599781288309,"__typename":"Ping"}}}}
# mutation response
< {"id":"2","type":"data","payload":{"data":{"ping":{"channel":"global","timestamp":1599781288309}}}} # mutation response
< {"id":"2","type":"complete"}