Skip to content

Instantly share code, notes, and snippets.

@taion taion/server.js
Last active Aug 16, 2019

Embed
What would you like to do?
GraphQL subscription server with Socket.IO, backed by Redis Pub/Sub
const redisClient = redis.createClient(REDIS_URL);
const listeners = Object.create(null);
function addListener(channel, listener) {
if (!listeners[channel]) {
listeners[channel] = [];
redisClient.subscribe(channel);
}
listeners[channel].push(listener);
return () => {
listeners[channel] = listeners[channel].filter(
item => item !== listener
);
if (!listeners[channel].length) {
redisClient.unsubscribe(channel);
delete listeners[channel];
}
};
}
redisClient.on('message', (channel, message) => {
const channelListeners = listeners[channel];
if (!channelListeners) {
return;
}
let body;
try {
body = JSON.parse(message);
} catch (e) {
console.error(`malformed message on ${channel}: ${message}`);
}
if (!body) {
return;
}
const data = getResponseData(body);
channelListeners.forEach((listener) => {
listener(data);
});
});
const io = new IoServer(server, {
serveClient: false,
});
io.on('connection', (socket) => {
// Turn the request into an Express request for consistency.
const req = Object.create((express: any).request);
Object.assign(req, socket.request, { app });
let authorization = null;
let credentials = null;
socket.on('authenticate', (token) => {
authorization = `Bearer ${token}`;
try {
const credentials = getCredentials(token);
} catch (e) {
credentials = null;
socket.emit('error', { code: 'invalid_token' });
}
});
const unsubscribeMap = Object.create(null);
socket.on('subscribe', async ({ id, query, variables }) => {
if (unsubscribeMap[id]) {
socket.emit('error', {
code: 'invalid_id.duplicate',
detail: id,
});
return;
}
function subscribe(channel) {
unsubscribeMap[id] = addListener(channel, async (body) => {
if (!authorizeRead(body, credentials)) {
// User is not authorized to view this update.
return;
}
const result = await graphql(
schema,
query,
body,
createContext(req, authorization),
variables,
);
socket.emit('subscription update', { id, ...result });
});
}
const result = await graphqlSubscribe({
schema,
query,
variables,
context: { subscribe },
});
if (result.errors) {
socket.emit('error', {
code: 'subscribe_failed',
detail: result.errors,
});
}
});
socket.on('unsubscribe', (id) => {
const unsubscribe = unsubscribeMap[id];
if (!unsubscribe) {
return;
}
unsubscribe();
delete unsubscribeMap[id];
});
socket.on('disconnect', () => {
Object.keys(unsubscribeMap).forEach((id) => {
unsubscribeMap[id]();
});
});
});
@Usamaliaquat123

This comment has been minimized.

Copy link

commented Sep 8, 2018

That's what i'm looking for . Thanks man 👍

@jeffreyyoung

This comment has been minimized.

Copy link

commented Sep 14, 2018

Where is graphqlSubscribe defined? Does it come from a library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.