Code Monkey home page Code Monkey logo

most-w3msg's Introduction

most.js W3C Messaging adapters

Create a most.js stream from a WebSocket (or SockJS, etc.), EventSource, MessagePort (or anything with the postMessage() API), or Worker and get all the filtering, transforming, reducing, etc goodness:

var fromWebSocket = require('most-w3msg').fromWebSocket;

var socket1 = new WebSocket('ws://localhost:8001');
var socket2 = new WebSocket('ws://localhost:8002');

// Merge 2 websockets into a single stream
// When the merged stream ends, both socket1.close and socket2.close
// will be called.
var stream = fromWebSocket(socket1, socket1.close.bind(socket1))
	.merge(fromWebSocket(socket2, socket2.close.bind(socket2));

// Log the first 10 items that meet the awesomeness criteria
// regardless of which socket they come from
stream.map(JSON.parse)
	.filter(function(item) {
		return item.awesomeness > 42;
	})
	.take(10)
	.forEach(function(awesomeItem) {
		console.log(awesomeItem);
	});

API

fromWebSocket

####fromWebSocket(webSocket [, dispose]) -> Stream

Create a stream from a WebSocket. The stream will contain all messages received by the WebSocket from the time a consumer begins observing the stream (using observe, .forEach or .reduce). The stream will end when the WebSocket closes, or will error if the WebSocket errors.

fromWebSocket does not close the WebSocket automatically. You can pass-in a dispose function if you want to close the socket when the stream ends or once all consumers of the stream have lost interest:

var stream = fromWebSocket(socket, socket.close.bind(socket));

toWebSocket

####toWebSocket(stream, webSocket) -> Promise

Send all events from a stream to a WebSocket, and return a promise for the end of the stream.

If the WebSocket closes before the stream ends, the returned promise will fulfill if the WebSocket closes cleanly, or will reject if the WebSocket errors. If the stream ends before the WebSocket closes, the returned promise will fulfill if the stream ends cleanly, or will reject if the stream errors.

toWebSocket does not close the WebSocket automatically. The creator of the WebSocket should close it.

fromEventSource

####fromEventSource(eventSource [, dispose]) -> Stream

fromEventSourceOn

####fromEventSourceOn(eventName, eventSource [, dispose]) -> Stream

fromMessagePort

####fromMessagePort(port [, dispose]) -> Stream

toMessagePort

####toMessagePort(stream, port) -> Promise

fromWorker

####fromWorker(worker [, dispose]) -> Stream

toWorker

####toWorker(worker, port) -> Promise

most-w3msg's People

Contributors

briancavalier avatar

Watchers

James Cloos avatar  avatar  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.