wdjungst / react-project

State of the art web development with React.js.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Help needed using socket.io

mattrei opened this issue · comments

Hi,

I am new to socket.io and I can't get it to work with react-project's Server API.
The client throws me http://localhost:8080/socket.io/?EIO=3&transport=polling&t=LDNeyEL errors. This seems to be related with a wrong server 'configuration'.

For the server.js I use this kind of code

const app = createServer(getApp)
app.start()
const server = require('http').createServer(app)
const io = require('socket.io')(server)

However maybe also I need to tell the React server routing to accecpt the /socket.io/ URL path.

May someone help out?
Thx,
Matthias

Not sure for this tool, but in general matter there are some issues with using socket.io webpack-dev-server, case they both use ws. Try to point your client script directly to your express backend, and dont try to use webpack as proxy.

there are two servers, the express server and webpack dev server, no proxy. By default the express server is at 8080 and webpack at 8081, so I don't think webpack is the problem here.

React Project shouldn't be getting in your way here, basically copy what's in the docs for socket.io here: http://socket.io/docs/

import createIo from 'socket.io'
import { createServer } from 'react-project/server'
const server = createServer(...)
const io = createIo(server)
server.start()

Hi, thanks for you answer.
However I think react-project gets in the way, because the Socket.IO API needs an require('http').Server() instance, not an require('express')() server instance as react-rpoject is exposing it.
So the problem is that in the .start() method a listen() is called, however for Socket.IO the listen has to be applied to the httpServer instance. So in my sense, the react-project API should expose the httpServer instance rather the expressServer instance.
http://socket.io/docs/

I could fix the problem by overriding the _listen method of the server instance, so that it does not call the listen() method.

Thanks