Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
add ability to broadcast to client sockets (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape authored Mar 24, 2018
1 parent 8efd4e6 commit 810fda2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,31 @@ Default: `{ context: process.cwd() }`
An object specifying the webpack [stats][stats] configuration. This does not
typically need to be modified.

## Communicating with Client WebSockets

In some rare situations, you may have the need to communicate with the attached
`WebSockets` in the browser. To accomplish this, open a new `WebSocket` to the
server, and send a `broadcast` message. eg.

```js
const stringify = require('json-stringify-safe');
const { WebSocket } = require('ws');

const socket = new WebSocket('ws://localhost:8081'); // this should match the server settings
const data = {
type: 'broadcast',
data: { // the message you want to broadcast
type: '<something fun>', // the message type you want to broadcast
data: { ... } // the message data you want to broadcast
}
};

socket.send(stringify(data));
```

_Note: The `data` property of the message should contain the enveloped message
you wish to broadcast to all other client `WebSockets`._

## Contributing

We welcome your contributions! Please have a read of [CONTRIBUTING.md](CONTRIBUTING.md) for more information on how to get involved.
Expand Down
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const stringify = require('json-stringify-safe');
const weblog = require('webpack-log');
const WebSocket = require('ws');
const HotClientError = require('./lib/HotClientError');
Expand Down Expand Up @@ -141,6 +142,18 @@ module.exports = (compiler, opts) => {
}
});

socket.on('message', (data) => {
const message = JSON.parse(data);

if (message.type === 'broadcast') {
for (const client of wss.clients) {
if (client.readyState === WebSocket.OPEN) {
client.send(stringify(message.data));
}
}
}
});

if (stats) {
const jsonStats = stats.toJson(options.stats);

Expand Down
4 changes: 4 additions & 0 deletions lib/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ socket(options, {
reload();
},

'window-reload': () => {
window.location.reload();
},

warnings(warnings) {
log.warn('Warnings while compiling.');

Expand Down
25 changes: 25 additions & 0 deletions test/tests/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ describe('Sockets', function d() {
});
});

it('should broadcast to child sockets', (done) => {
const socket = new WebSocket('ws://localhost:8081');
const socket2 = new WebSocket('ws://localhost:8081');

assert(socket);

socket.on('open', () => {
socket2.on('open', () => {
socket.on('message', (data) => {
const message = JSON.parse(data);

assert(message, 'test');
socket.close();
socket2.close();
done();
});

socket2.send(JSON.stringify({
type: 'broadcast',
data: 'test'
}));
});
});
});

it('sockets should receive warnings on change', (done) => {
// eslint-disable-next-line
const warningCode = '\nconsole.log(require)';
Expand Down

0 comments on commit 810fda2

Please # to comment.