How to use the createWebSocketStream function from ws

Find comprehensive JavaScript ws.createWebSocketStream code examples handpicked from public code repositorys.

4
5
6
7
8
9
10
11
12
13
To open a stream with `ws` on localhost:8099, just write:

```js
const WebSocket = require('ws')
const ws = new WebSocket('ws://localhost:8099')
const stream = WebSocket.createWebSocketStream(ws)
```

The readme for `ws` has more info if you're curious about how to
write the server side code: https://github.com/websockets/ws
fork icon326
star icon0
watch icon50

+ 5 other calls in file

21
22
23
24
25
26
27
28
29
30

wss = new WebSocket.Server({ port: PORT });
console.log('Server ready on port '+ PORT);
wss.on('connection', function connection(ws) {
    console.log('Socket connected. sending data...');
    const wsstream = WebSocket.createWebSocketStream(ws);
    //lets pipe into jmuxer stream, then websocket
    mp4Reader.pipe(jmuxer.createStream()).pipe(wsstream);
    
    ws.on('error', function error(error) {
fork icon99
star icon434
watch icon15

125
126
127
128
129
130
131
132
133
134
  });
}
ws.on('open', () => {
  logger.debug(`ws bind`);
  this._connectionCount = 0;
  const wsStream = WebSocket.createWebSocketStream(ws, {});
  let bh = bind(wsStream, tcpConn);
});
ws.on('close', (code, reason) => {
  logger.debug(`ws closed - ${code}`);
fork icon53
star icon65
watch icon13

1
2
3
4
5
6
7
8
9
10
const { WebSocketServer, createWebSocketStream } = require('ws')

const wss = new WebSocketServer({ port: 30104 })
wss.on('connection', (ws, req) => {
  console.log('Connected:', req.socket.remoteAddress)
  const stream = createWebSocketStream(ws, { encoding: 'utf8' })
  repl.start('<3os># ', stream)
  stream.pipe(process.stdout)
  process.stdin.pipe(stream)
})
fork icon10
star icon13
watch icon2

5
6
7
8
9
10
11
12
13
14
ws.on('close', () => process.exit(1))

ws.on('open', () => {
  if (program.pipe) {
    // Pipe
    var duplex = WebSocket.createWebSocketStream(ws)
    program.in.pipe(duplex)
    duplex.pipe(program.out)
  }
  else {
fork icon1
star icon7
watch icon1

201
202
203
204
205
206
207
208
209
```js
'use strict';
const WebSocket = module.require('ws');

const ws = new WebSocket('ws://localhost:8099');
const stream = WebSocket.createWebSocketStream(ws);
stream.pipe(process.stdout);
stream.write("hello\n");
```
fork icon0
star icon0
watch icon2

45
46
47
48
49
50
51
52
53
54
    headers: { 'x-auth-password': config['journal-token'] },
});

ws.on('open', () => {
    console.log(config['journal-fqdn'], 'websocket opened');
    const stream = WebSocket.createWebSocketStream(ws).
        pipe(new ParseJournalStream()).
        pipe(new FilterJournalDockerStream());
    stream.pause();
    resolve(stream);
fork icon0
star icon0
watch icon5

32
33
34
35
36
37
38
39
40
41
    webSocketStream.pipe(file);
}

module.exports.ex3 = function () {
   const ws = new webSocket('ws://localhost:4000/');
   const duplex = webSocket.createWebSocketStream(ws, {
       encoding: 'utf-8'
   });

   duplex.pipe(process.stdout);
fork icon0
star icon0
watch icon1

7
8
9
10
11
12
13
14
15
16



function on_wss_connection(wss){
    const socket = new net.Socket();
    const duplex = WebSocket.createWebSocketStream(wss);
    socket.connect(config["remote-port"], config["remote-host"]);

    socket.pipe(duplex);
    duplex.pipe(socket);
fork icon0
star icon0
watch icon2

2
3
4
5
6
7
8
9
10

const ws = new WebSocket('ws://localhost:4000');

let k = 0;
ws.on('open', ()=>{
    const duplex = WebSocket.createWebSocketStream(ws, {encoding: 'utf8'});
    let wfile = fs.createWriteStream(`./${++k}.txt`);
    duplex.pipe(wfile);
});
fork icon0
star icon0
watch icon1

2
3
4
5
6
7
8
9
10
const port = 4000;

new webSocket.Server({port}, () => {
    console.log(`WS started on port ${port}...`)
}).on('connection', ws => {
    const webSocketStream = webSocket.createWebSocketStream(ws);
    const file = fs.createWriteStream(`../upload/fileFromClient.txt`);
    webSocketStream.pipe(file);
});
fork icon0
star icon0
watch icon1