# stream-server

> A stream server in the browser

Latest version **0.5.1** (published 2012-08-15) · 0 weekly downloads

## Install

```sh
npm install stream-server
pnpm add stream-server
yarn add stream-server
bun add stream-server
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.1 |
| Published | 2012-08-15 |
| First published | 2012-08-15 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | Raynos |
| Maintainers | raynos |

## Links

- npm: https://www.npmjs.com/package/stream-server
- Repository: https://github.com/Colingo/stream-server
- Issues: https://github.com/Colingo/stream-server/issues
- npm.io page: https://npm.io/package/stream-server

## Dependencies (8)

- [ap](https://npm.io/package/ap.md) ~0.1.0
- [hat](https://npm.io/package/hat.md) ~0.0.3
- [xtend](https://npm.io/package/xtend.md) ~1.0.3
- [through](https://npm.io/package/through.md) ~0.1.4
- [duplexer](https://npm.io/package/duplexer.md) ~0.0.2
- [pause-stream](https://npm.io/package/pause-stream.md) ~0.0.3
- [stream-store](https://npm.io/package/stream-store.md) ~0.2.0
- [stream-router](https://npm.io/package/stream-router.md) ~0.1.0

## Recent versions

- 0.5.1 (latest) — 2012-08-15
- 0.5.0 — 2012-08-15

## README

# stream-server

A stream server in the browser

Create streaming servers in your browser. Emulating peer to peer connections through a central proxy server

## Example browser server (In a seperate browser)

``` js
var StreamServer = require("browser-stream-server")
    , boot = require("boot")
    , mdm = boot("/boot")

// Open a Stream server and listen on a server name
StreamServer(mdm, function (stream) {
    // Called every time a client opens up a stream connection to you
    stream.on("data", function (data) {
        console.log("[SERVER]", data)
    })
    stream.write("from server")
}).listen("SERVER_NAME")
```

## Example browser client (In a seperate browser)

``` js
var StreamClient = require("browser-stream-server")
    , boot = require("boot")
    , mdm = boot("/boot")

// Open a connection to a named stream server
var stream = StreamClient(mdm).connect("SERVER_NAME")
stream.on("data", function (data) {
    console.log("[CLIENT]", data)
})
stream.write("from client")
```

## Example proxy server

``` js
var boot = require("boot")
    , StreamServerProxy = require("browser-stream-server")
    , StreamRouter = require("stream-router")

// route stream traffic from boot through a stream router
var streamRouter = StreamRouter()
    , sock = boot(streamRouter)

// for every request to /browser-server let the StreamServer proxy handle it
streamRouter.addRoute("/browser-server/*"
    , StreamServerProxy("/browser-server"))

sock.install(someHttpServer, "/boot")
```

## Motivation

This a pre-runner to real P2P experiments that will be done once we can create [data channels][3] with WebRTC

This is the smallest piece necessary to spawn arbitary stream servers in browsers.

## Documentation

### `StreamClient(mdm, options).connect(serverName)`

To be used in the browser

``` js
var StreamClient = require("browser-stream-server")
var stream = StreamClient(mdm).connect("SERVER_NAME")
```

Returns a stream connection to that server name. It assumes the server is running somewhere else. If it's not then the stream is closed.

The `mdm` value is a [MuxDemux][4] stream. This can be got from mux-demux, mux-demux-shoe or boot.

options include a prefix option which allows you to set a custom prefix if the StreamServerProxy uses a custom prefix.

### `StreamServer(mdm, options, callback).listen(serverName)`

To be used in the browser

``` js
var StreamServer = require("browser-stream-server")
var server = StreamServer(mdm, options, function (stream) {
    // handle stream
}).listen("SERVER_NAME")
```

Creates a server stream connection. It assumes that named server is not already running somewhere else. If it is then the stream is closed.

The `mdm` value is a [MuxDemux][4] stream. This can be got from mux-demux, mux-demux-shoe or boot.

options are optional, you can pass a callback as the second parameter. The callback get's called every time someone else calls `.connect(serverName)` and you get passed a stream connection to them

options include a prefix option which allows you to set a custom prefix if the StreamServerProxy uses a custom prefix.

To close the server just `.end()` the returned server

### `StreamServerProxy(prefix)`

To be used in the server

``` js
var StreamServerProxy = require("browser-stream-server")
    , proxy = StreamServerProxy("/somePrefix")
// for every request to /browser-server let the StreamServer proxy handle it
streamRouter.addRoute("/somePrefix/*", proxy)
```

Set up a steam route handler to allow the stream servers and clients to be proxied through your central server.

Optionally pass in a prefix which defaults to `"/browser-server"`. If you pass in a different prefix make sure that your browser code matches the prefix.

#### proxy events

The returned proxy object emits `server-created` and `server-destroyed` events when a server stream connects and claims to own the SERVER_NAME

``` js
proxy.on("server.created", function (serverName) {
    createSomeSpecialResources(serverName)
})

proxy.on("server.destroyed", function (serverName) {
    destroySpecialResources(serverName)
})
```

#### `proxy.connect("serverName")`

``` js
var stream = proxy.connect(serverName)
```

You can manually connect directly to a stream server. This runs similar logic to `StreamClient(...).connect(serverName)`

## How it works

When you call `StreamServer(...).listen(SERVER_NAME)` you open a server stream to the `StreamServerProxy` and tell the server "redirect all connect SERVER_NAME traffic to me"

When you call `StreamClient(...).connect(SERVER_NAME)` you open a client stream to the `StreamServerProxy` saying "connect me to SERVER_NAME". The `StreamProxyServer` then sends a message to `StreamServer` identified by SERVER_NAME. The message contains a unique identifier, UNIQUE_ID for this client stream. The proxy server stores this client stream in memory.

The `StreamServer` receives this message and opens another connection to the `StreamServerProxy` saying "hi I'm server SERVER_NAME and want to connect to client UNIQUE_ID". The `StreamServerProxy` then gets the client stream out of memory and connects it to the new server connection which allows the client to talk to the server

## Installation

`npm install browser-stream-server`

## Contributors

 - Raynos

## MIT Licenced

  [1]: https://secure.travis-ci.org/Raynos/browser-stream-server.png
  [2]: http://travis-ci.org/Raynos/browser-stream-server
  [3]: http://dev.w3.org/2011/webrtc/editor/webrtc.html#widl-RTCPeerConnection-createDataChannel-DataChannel-DOMString-label-DataChannelInit-dataChannelDict
  [4]: https://github.com/dominictarr/mux-demux#muxdemuxoptions

---
_Source: https://npm.io/package/stream-server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
