I copied the instructions for express and substituted server with server.listener in the SubscriptionServer call. I keep getting error 400 in my react client when I try to connect with a react client.
Error during WebSocket handshake: Unexpected response code: 400
Got it working. Must provide the host and port when creating Hapi server. This is the full server code - updated the express example for creating a subscription to this.
```
import Hapi from 'hapi';
import { graphqlHapi, graphiqlHapi } from 'apollo-server-hapi';
import { execute, subscribe } from 'graphql';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import bodyParser from 'body-parser';
import cors from 'cors';
import { schema } from './src/schema';
async function startServer() {
const PORT = 4000;
const HOST = 'localhost';
const server = new Hapi.server({ host: HOST, port: PORT });
await server.register({
plugin: graphqlHapi,
options: {
path: '/graphql',
graphqlOptions: {
schema
},
route: {
cors: true
}
}
});
await server.register({
plugin: graphiqlHapi,
options: {
path: '/graphiql',
graphiqlOptions: {
endpointURL: '/graphql',
subscriptionsEndpoint: ws://${HOST}:${PORT}/subscriptions
},
}
});
new SubscriptionServer({
execute,
subscribe,
schema
}, {
server: server.listener,
path: '/subscriptions',
});
try {
await server.start();
} catch ( err ) {
console.log( error starting server: ${err.message});
}
console.log(Server running at : ${server.info.uri})
}
startServer();
Thanks I was looking for new SubscriptionServer :D
Most helpful comment
Got it working. Must provide the
hostandportwhen creating Hapi server. This is the full server code - updated the express example for creating a subscription to this.```
import Hapi from 'hapi';
import { graphqlHapi, graphiqlHapi } from 'apollo-server-hapi';
import { execute, subscribe } from 'graphql';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import bodyParser from 'body-parser';
import cors from 'cors';
import { schema } from './src/schema';
async function startServer() {
const PORT = 4000;
const HOST = 'localhost';
const server = new Hapi.server({ host: HOST, port: PORT });
await server.register({
plugin: graphqlHapi,
options: {
path: '/graphql',
graphqlOptions: {
schema
},
route: {
cors: true
}
}
});
await server.register({
plugin: graphiqlHapi,
options: {
path: '/graphiql',
graphiqlOptions: {
endpointURL: '/graphql',
subscriptionsEndpoint:
ws://${HOST}:${PORT}/subscriptions},
}
});
new SubscriptionServer({
execute,
subscribe,
schema
}, {
server: server.listener,
path: '/subscriptions',
});
try {
await server.start();
} catch ( err ) {
console.log(
error starting server: ${err.message});}
console.log(
Server running at : ${server.info.uri})}
startServer();