# osc-js

> OSC library for Node.js and the browser, with customizable Plugin API for WebSocket, UDP or bridge networking

Latest version **2.4.1** (published 2024-04-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install osc-js
pnpm add osc-js
yarn add osc-js
bun add osc-js
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.1 |
| Published | 2024-04-21 |
| First published | 2017-01-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 271.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 283 |
| Author | Andreas Dzialocha |
| Maintainers | andreasdz |
| Keywords | osc, data, open, sound, control, websocket, udp, datagram, network |

## Links

- npm: https://www.npmjs.com/package/osc-js
- Repository: https://github.com/adzialocha/osc-js
- Homepage: https://github.com/adzialocha/osc-js#readme
- Issues: https://github.com/adzialocha/osc-js/issues
- npm.io page: https://npm.io/package/osc-js

## Dependencies (1)

- [ws](https://npm.io/package/ws.md) ^8.16.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 2.4.1 (latest) — 2024-04-21
- 2.4.0 — 2022-10-07
- 2.3.2 — 2022-04-14
- 2.3.1 — 2022-04-01
- 2.3.0 — 2022-02-16
- 2.2.0 — 2021-12-02
- 2.1.2 — 2021-01-03
- 2.1.1 — 2021-01-03
- 2.1.0 — 2019-11-26
- 2.0.4 — 2019-09-19
- 2.0.3 — 2019-03-12
- 2.0.2 — 2018-11-02
- 2.0.1 — 2018-11-01
- 2.0.0 — 2018-11-01
- 1.2.2 — 2018-01-18
- … 10 more at https://npm.io/package/osc-js/versions

## README

osc-js
======

<p>
  <a href="https://github.com/adzialocha/osc-js/actions">
    <img src="https://github.com/adzialocha/osc-js/workflows/Node.js%20CI/badge.svg" alt="Build status">
  </a>
  <a href="https://www.npmjs.org/package/osc-js">
    <img src="https://img.shields.io/npm/v/osc-js.svg?style=flat-square" alt="npm version">
  </a>
  <a href="http://spdx.org/licenses/MIT">
    <img src="https://img.shields.io/npm/l/osc-js.svg?style=flat-square" alt="npm licence">
  </a>
  <a href="https://adzialocha.github.io/osc-js">
    <img src="https://raw.githubusercontent.com/adzialocha/osc-js/gh-pages/badge.svg" alt="ESDoc status">
  </a>
</p>

osc-js is an [Open Sound Control](http://opensoundcontrol.org/) library for JavaScript applications (UMD module for Node, Browser etc.) with address pattern matching and timetag handling. Sends messages via *UDP*, *WebSocket* or both (bridge mode) and offers a customizable Plugin API for network protocols.

[Wiki](https://github.com/adzialocha/osc-js/wiki) | [Basic Usage](https://github.com/adzialocha/osc-js/wiki/Basic-Usage) | [Documentation](https://adzialocha.github.io/osc-js) | [Plugin API](https://github.com/adzialocha/osc-js/wiki/Plugin-API)

## Features

- UMD Module running in Node.js, Electron, Chrome Apps, browser or any other JS environment
- Can be used with Webpack and Browserify
- TypeScript definitions
- No dependencies (except of `ws` in Node.js or similar environments)
- Receive sender information from incoming messages
- Built-in *UDP*, *WebSocket* networking support as plugins
- Special bridge plugin for easy communication between *UDP*- and *WebSocket* clients
- Plugin API for custom network protocols
- Featuring all [OSC 1.0 specifications](http://opensoundcontrol.org/spec-1_0)
- OSC Address pattern matching
- Time-critical OSC Bundles with Timetags
- Extended (nonstandard) argument types

## Documentation

Read more about osc-js and how to use it in the [Wiki](https://github.com/adzialocha/osc-js/wiki) and [Documentation](https://adzialocha.github.io/osc-js).

## Example

```js
const osc = new OSC()

osc.on('/param/density', (message, rinfo) => {
  console.log(message.args)
  console.log(rinfo)
})

osc.on('*', message => {
  console.log(message.args)
})

osc.on('/{foo,bar}/*/param', message => {
  console.log(message.args)
})

osc.on('open', () => {
  const message = new OSC.Message('/test', 12.221, 'hello')
  osc.send(message)
})

osc.open({ port: 9000 })
```

## Installation and Usage

Recommended installation via npm: `npm i osc-js` or `yarn add osc-js`.

Import the library `const OSC = require('osc-js')` or add the script `lib/osc.js` or `lib/osc.min.js` (minified version) for usage in a browser.

## Plugins

osc-js offers a plugin architecture for extending it's networking capabilities. The library comes with four built-in plugins. This is propably all you need for an OSC application:

- `WebsocketClientPlugin` (default)
- `WebsocketServerPlugin`
- `DatagramPlugin` for UDP network messaging
- `BridgePlugin` useful Bridge between WebSocket- and UDP Clients

Configuration and examples of every plugin can be read here: [Wiki](https://github.com/adzialocha/osc-js/wiki).

### Example: WebSocket Server

Register the plugin when creating the OSC instance:

```js
const osc = new OSC({ plugin: new OSC.WebsocketServerPlugin() })
osc.open() // listening on 'ws://localhost:8080'
```

### Example: OSC between MaxMSP/PD/SC etc. and your browser

1. Write a simple webpage. The library will use a WebSocket client
by default.

  ```html
  <button id="send">Send Message</button>
  <script type="text/javascript" src="lib/osc.min.js"></script>
  <script type="text/javascript">
    var osc = new OSC();
    osc.open(); // connect by default to ws://localhost:8080

    document.getElementById('send').addEventListener('click', () => {
      var message = new OSC.Message('/test/random', Math.random());
      osc.send(message);
    });
  </script>
  ```

2. Write a Node app (the "bridge" between your UDP and WebSocket clients).

  ```js
  const OSC = require('osc-js')

  const config = { udpClient: { port: 9129 } }
  const osc = new OSC({ plugin: new OSC.BridgePlugin(config) })

  osc.open() // start a WebSocket server on port 8080
  ```

3. Create your Max/MSP patch (or PD, SuperCollider etc.).

  ```
  [udpreceive 9129] // incoming '/test/random' messages with random number
  ```

### Custom solutions with Plugin API

It is possible to write more sophisticated solutions for OSC applications without loosing the osc-js interface (including its message handling etc.). Read the [Plugin API documentation](https://github.com/adzialocha/osc-js/wiki/Plugin-API) for further information.

```js
class MyCustomPlugin {
  // ... read docs for implementation details
}

const osc = new OSC({ plugin: MyCustomPlugin() })
osc.open()

osc.on('/test', message => {
  // use event listener with your plugin
})
```

### Usage without plugins

The library can be used without the mentioned features in case you need to write and read binary OSC data. See this example below for using the [Low-Level API](https://github.com/adzialocha/osc-js/wiki/Low-Level-API) (even though the library already has a solution for handling UDP like in this example):

```js
const dgram = require('dgram')
const OSC = require('osc-js')

const socket = dgram.createSocket('udp4')

// send a messsage via udp
const message = new OSC.Message('/some/path', 21)
const binary = message.pack()
socket.send(new Buffer(binary), 0, binary.byteLength, 41234, 'localhost')

// receive a message via UDP
socket.on('message', data => {
  const msg = new OSC.Message()
  msg.unpack(data)
  console.log(msg.args)
})
```

## Development

osc-js uses [Babel](http://babeljs.io) for ES6 support, [ESDoc](https://esdoc.org) for documentation, [Mocha](https://mochajs.org/) + [Chai](http://chaijs.com/) for testing and [Rollup](https://rollupjs.org) for generating the UMD module.

Clone the repository and install all dependencies:

```
git clone git@github.com:adzialocha/osc-js.git
cd osc-js
npm install
```

### Testing

`npm run test` for running the tests.
`npm run test:watch` for running specs during development. Check code style with `npm run lint`.

### Deployment

`npm run build` for exporting UMD module in `lib` folder.

### Contributors

* [@adzialocha](https://github.com/adzialocha)
* [@davidgranstrom](https://github.com/davidgranstrom)
* [@elgiano](https://github.com/elgiano)
* [@eliot-akira](https://github.com/eliot-akira)
* [@JacobMuchow](https://github.com/JacobMuchow)
* [@PeterKey](https://github.com/PeterKey)
* [@yaxu](https://github.com/yaxu)
* [@yojeek](https://github.com/yojeek)

### ESDocs

`npm run docs` for generating a `docs` folder with HTML files documenting the library. Read them online here: [https://adzialocha.github.io/osc-js](https://adzialocha.github.io/osc-js)

## License

MIT License `MIT`

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