Giter Site home page Giter Site logo

websocket-stream's Introduction

websocket-stream

npm install websocket-stream

use HTML5 websockets the node way -- with streams

in the browser

you can use browserify to package this module for browser use.

var websocket = require('websocket-stream')
var ws = websocket('ws://realtimecats.com')
ws.pipe(somewhereAwesome)

ws is a stream and speaks stream events: data, error and end. that means you can pipe output to anything that accepts streams. you can also pipe data into streams (such as a webcam feed or audio data)

browserify steps

npm install -g browserify // install browserify
cd node_modules/websocket-stream
npm install . // install dev dependencies
browserify index.js -s websocket-stream > websocket-stream.js // require websocket-stream.js in your client-side app

on the server

using the ws module you can make a websocket server and use this module to get websocket streams on the server:

var WebSocketServer = require('ws').Server
var websocket = require('websocket-stream')
var wss = new WebSocketServer({server: someHTTPServer})
wss.on('connection', function(ws) {
  var stream = websocket(ws)
  fs.createReadStream('bigdata.json').pipe(stream)
})

options

pass in options as the second argument like this:

websocketStream('ws://foobar', { type: someTypedArray })

// e.g. {type: Uint8Array} means you'll get Uint8Arrays back instead of ArrayBuffers

possible options are...

{ 
  protocol: // optional, string, specify websocket protocol
  type: // optional, TypedArray object, wraps the ArrayBuffer before emitting
}

binary sockets

To send binary data just write a Buffer or TypedArray to the stream. On the other end you will receive Buffer instances if it's the server and ArrayBuffer instances if it's the client. The client will default to ArrayBuffer objects but can also be configured to receive Blobs.

If you write binary data to a websocket on the server, the client will receive binary objects. Same thing goes for strings.

license

BSD LICENSE

websocket-stream's People

Contributors

max-mapper avatar mcollina avatar juliangruber avatar raynos avatar piatra avatar rawberg avatar

Stargazers

Roman avatar

Watchers

Philip Nuzhnyi avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.