question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Socket.io + GraphQL Yoga Server = socketID undefined 404 (Not Found)

See original GitHub issue

Running out of ideas what needs to be done to make GraphQL-Yoga Server to finally work with socket.io:

In browser’s console: image

Client

NextJS CRA Component:

import React, { Component } from 'react';
import io from 'socket.io-client';
socket = io('https://localhost:4444');

export default class OAuthWidget extends Component {
  state = {};
  componentDidMount() {
    
    console.log( {socket} );
    fetch('https://localhost:4444/').then(res => {
      if (res) {
        console.log('Response from server received');
      }
    });
  }

  render() {
    console.log('socket: ', socket);   // returns undefined

    return <div>Socket id: {socket}</div>;
  }
}

NodeJS, GraphQLServer

(irrelevant cut out for sake of brevity):

index.js:

const fs = require('fs');
const express = require('express');
const session = require('express-session');
const socketio = require('socket.io');
const cookieParser = require('cookie-parser');
const createServer = require('./createServer');

const server = createServer();

server.express.use(express.json());

server.express.use(
  session({
    secret: process.env.SESSION_SECRET,
    resave: true,
    saveUninitialized: true
  })
);

const io = socketio(server);
server.express.set('io', io);
socketio.listen(server);

server.express.use(cookieParser());

server.start(
  {
    cors: {
      credentials: true,
      origin: ['http://localhost:3000']
    },
    https: {
      key: fs.readFileSync(`./src/ssl/server.key`),
      cert: fs.readFileSync(`./src/ssl/server.crt`)
    }
  },
  async details => {
    console.log(`Server started on: https://localhost:${details.port}`);
  }
);

./createServer.js:

const { GraphQLServer, PubSub } = require('graphql-yoga');
..
function createServer() {
  return new GraphQLServer({
    typeDefs: 'src/schema.graphql',
    resolvers: {
      Query,
      Mutation
    },
    resolverValidationOptions: {
      requireResolversForResolveType: false
    },
    context: req => ({
      ...req,
      db,
      pubsub
    })
  });
}

module.exports = createServer;

what is missing or wrong here? In official examples we have to create http server and then call .listen() on it passing the express() app :

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(80);

I have declared PORT=4444 constant in process.env variables, I have also tried including it directly into options, no changes

many thanks in advance folks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

3reactions
LionelPauluscommented, May 17, 2019

Hey @schickling, do you have an idea for this problem? For production environnement we would really like having graphql-yoga and socket.io on the same port.

1reaction
cuplizcommented, Oct 16, 2020

since the server.start return server instance. we can use that to init io, ex:

const server = createServer();
const options = {
    cors: {
      credentials: true,
      origin: ['http://localhost:3000']
    },
    https: {
      key: fs.readFileSync(`./src/ssl/server.key`),
      cert: fs.readFileSync(`./src/ssl/server.crt`)
    },
   port: 4444
}
const init = async () => {
  const serverio = await server.start(options, () => console.log( `Server is running on port http://localhost: 4444 ` ))
  const io = socketio(serverio)
  io.sockets.on('connection', (socket) => {
    console.log('connect', socket.id)
  })
}
init()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Socket.io + GraphQL Yoga Server = socketID undefined 404 ...
Running out of ideas what needs to be done for this to finally work: In browser's console: Client NextJS CRA Component: import React, ......
Read more >
GET/ socket io not found 404 - node.js - Stack Overflow
The problem is in your server code. You bind socket.io to http , but only app listens on the port (5000). You need...
Read more >
Subscriptions in Apollo Server - Apollo GraphQL Docs
To run both an Express app and a separate WebSocket server for subscriptions, we'll create an http.Server instance that effectively wraps the two...
Read more >
W3layouts cms exploit - Weebly
løsningerUva 419 Page Udløbet 42 42 mib til bits 494291269 4asfassadasd 4chan ... authentication apollo graphql mutation hook Apollo server App Development ...
Read more >
posts-2019-02-06.xml - Medium
Earth/dry-shampoos-are-easy-but-not-healthy-for-your-hair-6a73ece3d1f2 ... -findings-that-shouldve-been-on-the-front-page-774a3fd702a 2019-02-06 monthly 0.2 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found