How to use websocket-stream - 10 common examples

To help you get started, we’ve selected a few websocket-stream examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github yldio / pouch-websocket-sync / server.js View on Github external
function createServer(httpServer, onRequest) {
  if (! httpServer) {
    throw new Error('need a base HTTP server as first argument');
  }
  var wsserver = websocket.createServer({server: httpServer}, handle);
  return wsserver;

  function handle(stream) {
    stream.on('error', propagateError);
    var server = PouchSync.createServer(onRequest);
    server.on('error', propagateError);
    stream.pipe(server).pipe(stream);
  }

  /* istanbul ignore next */
  function propagateError(err) {
    if (ignoreErrorMessages.indexOf(err.message) < 0) {
      wsserver.emit('error', err);
    }
  }
}
github netbeast / dashboard / index.js View on Github external
server.listen(process.env.PORT, function () {
    const addr = server.address().address
    const port = server.address().port
    console.log('👾  Netbeast dashboard started on %s:%s', addr, port)
    // attach mqtt broker to websockets stream
    websocket.createServer({ server: server }, aedes.handle)
    bootOnload()
    if (process.send) process.send('ready') //If this file has been called from electron.js
  })
})
github arso-project / archipel / packages / backend / server.js View on Github external
api: {
      hyperlib
    },
    rpc: rpcApis
  })

  // server
  const staticServe = ecstatic({
    root: config.server.static,
    showDir: false
  })

  const server = http.createServer(staticServe)

  // websocket rpc
  const wss = websocket.createServer({ server }, (stream, request) => {
    request.on('error', err => console.error('request error: ', err))
    stream.on('error', err => console.error('stream error: ', err))

    const reqUrl = url.parse(request.url)

    if (reqUrl.pathname !== '/api') return

    const transport = streambus()
    pump(stream, transport.stream, stream)
    api.addPeer(transport).then(peer => {
      console.log('session established', peer.api)
    })
  })

  // Handle errors gracefully.
  // todo: find out what really is needed. kept getting ECONNRESET errors from TCP.onstreamread
github mcollina / aedes / test / connect.js View on Github external
test('websocket clients have access to the request object', function (t) {
  t.plan(3)

  var broker = aedes()
  var server = http.createServer()
  ws.createServer({
    server: server
  }, broker.handle)

  server.listen(4883, function (err) {
    t.error(err, 'no error')
  })

  broker.on('client', function (client) {
    if (client.req) {
      t.pass('client request object present')
      if (client.req.headers) {
        t.equal('sample', client.req.headers['x-test-protocol'])
        finish()
      }
    } else {
      t.fail('no request object present')
github maxogden / electron-microscope / bridge.js View on Github external
function createSocket (server) {
    var emitter = new events.EventEmitter()
    emitter.httpServer = server
    emitter.websocketServer = websocket.createServer({server: emitter.httpServer}, handleSocket)

    function handleSocket (socket) {
      emitter.activeSocket = ndjson.serialize()
      pump(emitter.activeSocket, debugStream('to client: %s'), socket)
      var handlerStream = through.obj(function(data, enc, next) {
        debug('thats it', data)
        if (!data.id) {
          console.error('invalid message ' + d)
          return next()
        }
        if (data.ready) {
          emitter.emit(data.id + '-ready')
          return next()
        } else if (data.error) {
          emitter.emit(data.id + '-error', data.error)
          return next()
github workshopper / stream-adventure / problems / websockets / index.js View on Github external
exports.verify = verify({ modeReset: true }, function (args, t) {
    t.plan(1);
    var server = createServer(path.resolve(args[0]));
    t.once('end', function () {
        server.close();
        wss.close();
    });
    
    var wss = wsock.createServer({ server: server }, handle);
    function handle (stream) {
        stream.pipe(split()).pipe(through(function (buf, enc, next) {
            t.equal(buf.toString(), 'hello');
            stream.end();
        }));
    }
});
github yldio / pouch-websocket-sync-example / web / websocket-server.js View on Github external
const http = require('http');
const websocket = require('websocket-stream')
const PouchStreamServer = require('pouch-stream-server');
const PouchDB = require('pouchdb');
const PipeChannels = require('pipe-channels');

const server = http.createServer();
const wss = websocket.createServer({server: server}, handle);

const db = new PouchDB('todos-server');
const allowedDatabases = ['todos-server'];

server.listen(3001, function() {
  console.log((new Date()) + ' Server is listening on', server.address());
});

function handle(stream) {
  const channelServer = PipeChannels.createServer();
  const pouchServer = PouchStreamServer();
  pouchServer.dbs.add('todos-server', db);

  channelServer.on('request', function(req) {
    if (allowedDatabases.indexOf(req.payload.database) >= 0) {
      req.deny('database not allowed');
github fl4re / rest3d-new / example / server.js View on Github external
stream.on('data', function(message){
		if(VERBOSE){
			console.log('------------------------------ DATA RECEIVED -------------------------------');
		}
		assetManager.feedbackManager(message);
	});

	stream.on('end', function(){
		if(VERBOSE){
			console.log('--------------------------- TRANSMITION CLOSED -----------------------------');
		}
	});
};

var server = app.listen(8080);
websocket.createServer({server: server}, handle);
github mcollina / aedes / example.js View on Github external
'use strict'

var aedes = require('./aedes')()
var server = require('net').createServer(aedes.handle)
var httpServer = require('http').createServer()
var ws = require('websocket-stream')
var port = 1883
var wsPort = 8888

server.listen(port, function () {
  console.log('server listening on port', port)
})

ws.createServer({
  server: httpServer
}, aedes.handle)

httpServer.listen(wsPort, function () {
  console.log('websocket server listening on port', wsPort)
})

aedes.on('clientError', function (client, err) {
  console.log('client error', client.id, err.message, err.stack)
})

aedes.on('connectionError', function (client, err) {
  console.log('client error', client, err.message, err.stack)
})

aedes.on('publish', function (packet, client) {
github esri-es / arcgis_websocket_server / streamserver / streamserver_simple.js View on Github external
console.log(`Found spatial information in fields [${lonField},${latField}]`);
  }

  let HTTPServer = _setupHTTPServer(conf.service);
  let wsRemoteClient = websocket(conf.ws.client.wsUrl, {
    perMessageDeflate: false
  });

  var fieldGeo = avoidGeo
    ? null
    : {
      lat : latField,
      lon : lonField
    };

  var wss = websocket.createServer({
    server: HTTPServer,
    path : `${conf.service.base_url}/subscribe`,
    binary: false },
    _setupSource({
      pullStream : wsRemoteClient,
      service: conf.service,
      geo : fieldGeo
    }))
}

websocket-stream

Use websockets with the node streams API. Works in browser and node

BSD-2-Clause
Latest version published 5 years ago

Package Health Score

53 / 100
Full package analysis

Popular websocket-stream functions