Gerçek Zamanlı Veriler için GraphQL Abonelikleri
Abonelikleri kullanarak GraphQL API'nize gerçek zamanlı özellikler ekleyin; böylece istemciler, veriler değiştiği anda WebSockets üzerinden canlı güncellemeleri alır.
Gerçek Zamanlı Veriler için GraphQL Abonelikleri, CoddyKit'te ücretsiz bir Node.js Backend Development Bootcamp dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Node.js Backend Development Bootcamp öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Node.js Backend Development Bootcamp kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
The Three GraphQL Operations
GraphQL defines three root operation types:
- Query: read data
- Mutation: change data
- Subscription: receive a stream of live updates
Queries and mutations are request/response; subscriptions keep a connection open for ongoing events.
When to Use Subscriptions
Subscriptions shine when clients need real-time data:
- Chat messages
- Live notifications
- Collaborative editing
- Live dashboards and scores
For data that rarely changes, polling a query is simpler.
WebSockets Under the Hood
Queries and mutations travel over HTTP, but subscriptions need a persistent two-way channel — a WebSocket. The server pushes events to subscribed clients without them asking again.
Defining a Subscription in the Schema
Add a Subscription type to your schema with fields clients can subscribe to. Each field is an event stream returning some type.
type Subscription {
messageAdded(channelId: ID!): Message!
}The PubSub Engine
Subscriptions rely on a publish/subscribe system. A PubSub instance lets your resolvers publish events and your subscriptions listen for them.
const { PubSub } = require('graphql-subscriptions');
const pubsub = new PubSub();Publishing an Event
Inside a mutation, after saving data, publish an event on a named topic. Anyone subscribed to that topic receives the payload.
const message = await Message.create(input);
pubsub.publish('MESSAGE_ADDED', { messageAdded: message });
return message;The Subscription Resolver
A subscription resolver returns an async iterator via asyncIterator, telling the server which topic to forward to the client.
const resolvers = {
Subscription: {
messageAdded: {
subscribe: () => pubsub.asyncIterator('MESSAGE_ADDED')
}
}
};Filtering Events
Often a client only cares about events for one channel. withFilter wraps the iterator and forwards only events that match the subscription's arguments.
const { withFilter } = require('graphql-subscriptions');
subscribe: withFilter(
() => pubsub.asyncIterator('MESSAGE_ADDED'),
(payload, vars) => payload.messageAdded.channelId === vars.channelId
)Client Subscription Query
The client writes a subscription operation just like a query, but with the subscription keyword. The server streams results as they happen.
subscription OnMessage($id: ID!) {
messageAdded(channelId: $id) {
id
text
}
}Scaling Across Servers
The in-memory PubSub only works on a single instance. To broadcast across many servers, swap in a backend like graphql-redis-subscriptions so events reach all connected clients.
const { RedisPubSub } = require('graphql-redis-subscriptions');
const pubsub = new RedisPubSub();Cleaning Up Connections
Open WebSockets consume resources. The server should detect disconnects and stop streaming. Apollo Server handles much of this, but watch for orphaned subscriptions and authenticate the connection on setup.
Quick Check
Test your GraphQL subscription knowledge.
Recap
You added real-time data with GraphQL subscriptions:
- Subscriptions stream live events over WebSockets
- Declare a
Subscriptiontype and resolve it withasyncIterator - Mutations
publishevents;withFiltertargets the right clients - Scale across servers with a Redis-backed PubSub and clean up connections
Now your API can power chat, notifications, and live dashboards.
Sıkça Sorulan Sorular
“Gerçek Zamanlı Veriler için GraphQL Abonelikleri” dersi ücretsiz mi?
Evet — “Gerçek Zamanlı Veriler için GraphQL Abonelikleri” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Node.js Backend Development Bootcamp kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Node.js Backend Development Bootcamp kursu toplamda 4 dersten oluşur.
“Gerçek Zamanlı Veriler için GraphQL Abonelikleri” dersinde ne öğreneceğim?
Abonelikleri kullanarak GraphQL API'nize gerçek zamanlı özellikler ekleyin; böylece istemciler, veriler değiştiği anda WebSockets üzerinden canlı güncellemeleri alır. Node.js Backend Development Bootcamp ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Node.js Backend Development Bootcamp öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Node.js Backend Development Bootcamp, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.
“Gerçek Zamanlı Veriler için GraphQL Abonelikleri” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Node.js Backend Development Bootcamp dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Node.js Backend Development Bootcamp dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Node.js ile Sunucusuz Mimarilere Giriş
- GraphQL API Tasarım İlkeleri
- Apollo ile GraphQL Sunucusu Oluşturma
- Gerçek Zamanlı Veriler için GraphQL Abonelikleri