Websockets with new Isomorphic Hot Module Replacement
See original GitHub issueHello,
I’m looking for a way to integrate websockets into an app with current version of HMR, after changes being made to the start script.
In previous version I could easily access http server to attach WS capability (exactly Apollo Subscriptions server for GraphQL Subscriptions)
// server.js
//...
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
// ...
const server = createServer(app);
server.listen(config.port, () => {
console.info(`The server is running at http://localhost:${config.port}/`);
SubscriptionServer.create({
subscriptionManager,
}, {
server,
path: config.subscriptions.path,
});
});
With current way the HMR is done Browsersync’s server is used. I was thinking to attach own websocket server directly to Browsersync’s by changing a little start script like:
// start.js
// ...
// Launch the development server with Browsersync and HMR
const b = await new Promise((resolve, reject) => browserSync.create().init({
// https://www.browsersync.io/docs/options
server: 'src/server.js',
middleware: [server],
open: !process.argv.includes('--silent'),
...isDebug ? {} : { notify: false, ui: false },
}, (error, bs) => (error ? reject(error) : resolve(bs))));
SubscriptionServer.create({
subscriptionManager,
}, {
b.server,
path: config.subscriptions.path,
});
this enables GraphQL subscriptions but sadly breaks socket.io in Browsersync, so no HMR then (probably overrides socket.io from BS server).
I’m looking for a hint how could it be implemented with current setup, without Browsersync’s proxy. Is it at all possible to be done this way?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:17 (2 by maintainers)
Top Results From Across the Web
Isomorphic Implementation of React | by Yudhajit Adhikary
Isomorphic process is a process of rendering web-application on browser in ... Hot Module Replacement is the process by application gets updated without ......
Read more >Differences between socket.io and websockets - Stack Overflow
I wrote an npm module to demonstrate the difference between WebSocket and Socket.IO: ... appendChild(i); } log('opening websocket connection'); var s = new...
Read more >react-isomorphic-render - npm
Start using react-isomorphic-render in your project by running `npm i ... Supports Webpack "hot reload" (aka "Hot Module Replacement") ...
Read more >Webpack's Hot Module Replacement Feature Explained
The Webpack compiler will build your application with the changes, creating a new manifest file and comparing it with the old one. This...
Read more >The future of web software is HTML over WebSockets
It seems like you could get most, if not all the way, there with an "isomorphic" Redux store on client and server side,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ricardomoura I don’t know if this will be still useful for you, but I had the same problem after reloads as it was not always calling
dispose
from hot reload (depending which file I was exactly editing). Sadly I still didn’t have luck to make subscriptions working on the same port as the server (maybe this could help solve this problem https://github.com/websockets/ws/pull/885), but for now I’ve solvedEADDRESS
, by closing previous subscription server not using webpack’s hot moduleaccept/dispose
but inside ofstart.js
script. Code looks like:And the last part is an update in
tools/start.js
to close previously opened websocket server:It’s important that on production it will be working under the same port as your app, so for e.g. if you have set a port
3000
then it will also listen for subscriptions under the port3000
. Locally you will have to setup port inside of a config and use other one for subscriptions.It would be great if somebody could share a better solution.
@Asthor I solved this problem by giving the socket server it’s own port and then managing it’s creation/destruction via HMR’s dispose/accept functions like what @lobnico proposed.
Doing it this way accomplishes two things:
Assuming you have the latest version of RSK (after start.js got redone)
Add a value for your socket port in /src/config.js (I call it subPort)
Create /src/core/server/subscriptions.js
Then modify /src/server.js
You can verify that it’s working by going to graphiql and looking through the dev console. If you don’t see some variation of
handshake failed
, then it’s probably working. In chrome, if you go indev console -> network
and find the subscription request, look in the frames tab to further verify your subscriptions are working.This one was a bit of a headscratcher, there are probably better ways to solve this problem but here’s where I said “It’s good enough”. Many thanks to @lobnico for getting me 80% of the way to working subscriptions 😃