# Oscjs meets Sonic Pi - Incoming Port number

**URL:** <https://in-thread.sonic-pi.net/t/oscjs-meets-sonic-pi-incoming-port-number/3859>\
**Category:** Support, Help & Resources\
**Created:** [May 18, 2020, 2:09pm UTC](https://in-thread.sonic-pi.net/t/oscjs-meets-sonic-pi-incoming-port-number/3859 "2020-05-18T14:09:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [May 18, 2020, 2:09pm UTC](https://in-thread.sonic-pi.net/t/oscjs-meets-sonic-pi-incoming-port-number/3859/1 "2020-05-18T14:09:43Z")

</div>

HI everybody,

After the questions @dewildequinten about ocsjs, i do some researches to test ocsjs library.

i struggle with osc ports number.

so if somebody want to test i made a repo : [https://github.com/nlebellier/ocsjsMeetsSonicPi](https://github.com/nlebellier/ocsjsMeetsSonicPi)

1 - we have to launch node before sonic-pi  
2 - then sonicpi

It appears that the incoming osc port can’t be 4560 as usual so a randomized number is set by sonic pi. Can someone confirm this ?

If @robin.newman has some brilliant comments to add, he is welcomed or anybody else of course.

Cheers,

* * *

Nicolas

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [May 18, 2020, 4:09pm UTC](https://in-thread.sonic-pi.net/t/oscjs-meets-sonic-pi-incoming-port-number/3859/2 "2020-05-18T16:09:49Z")

</div>

You’ve got your ports mixed up.  
If you alter your index.js file as below

```ruby
var osc = require("osc");

/ ****************
 * OSC Over UDP *
 **************** /

var getIPAddresses = function () {
    var os = require("os"),
        interfaces = os.networkInterfaces(),
        ipAddresses = [];

    for (var deviceName in interfaces) {
        var addresses = interfaces[deviceName];
        for (var i = 0; i < addresses.length; i++) {
            var addressInfo = addresses[i];
            if (addressInfo.family === "IPv4" && !addressInfo.internal) {
                ipAddresses.push(addressInfo.address);
            }
        }
    }

    return ipAddresses;
};

var udpPort = new osc.UDPPort({
    localAddress: "0.0.0.0",
    localPort: 9000,

	// below is the destination target, ie SonicPi ip and port
    remoteAddress: "127.0.0.1",
    remotePort: 4560,
    metadata: true
	
});

udpPort.on("ready", function () {
    var ipAddresses = getIPAddresses();
    console.log("Listening for OSC over UDP.");
    ipAddresses.forEach(function (address) {
        console.log(" Host:", address + ", Port:", udpPort.options.localPort);
    });
});

udpPort.on("message", function (oscMessage) {
    console.log(oscMessage);
});

udpPort.on("error", function (err) {
    console.log(err);
});

udpPort.open();

/ ***********send to sonic pi***** /
// Every second, send an OSC message to Sonic Pi - 1000 ms = 1s :-)
setInterval(function() {
    var msg = {
        address: "/play/note",
        args: [
            {
 		    // midi note value 
		        type: "i",
                value: 60+24
            },
            { // amp value a float to send decimal numbers
                type: "f",
                value: 1.5
            },
	        { // sustain value
		        type: "f",
		        value: 2
		    }
        ]
    };

    console.log("Sending message", msg.address, msg.args, "to", udpPort.options.remoteAddress + ":" + udpPort.options.remotePort);
    udpPort.send(msg);
}, 1000);

```

and then use this in Sonic Pi to receive it works fine.

```ruby
live_loop :test do
  n = sync "/osc*/play/note"
  puts play n[0],amp: n[1],sustain: n[2]
end

```

ADDENDUM I chose 9000 arbitrarily as the port associated with the node sender. Could be anything . The important thing is that Sonic Pi receives on port 4560. You had allocated this to the node app and so it wasn’t available for Sonic Pi.

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [May 18, 2020, 5:56pm UTC](https://in-thread.sonic-pi.net/t/oscjs-meets-sonic-pi-incoming-port-number/3859/3 "2020-05-18T17:56:44Z")

</div>

Thank you @robin.newman  
it’s now clear in my head and i forgot the use of \*

The repo is updated.  
Cheers

---

<div class="post-metadata">

**Author:** ![dewildequinten](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@dewildequinten](https://in-thread.sonic-pi.net/u/dewildequinten)\
**Post date:** [May 22, 2020, 12:14pm UTC](https://in-thread.sonic-pi.net/t/oscjs-meets-sonic-pi-incoming-port-number/3859/4 "2020-05-22T12:14:12Z")

</div>

@nlb @robin.newman

Guys! Thanks so much for clearing this out for me!! It works perfectly.

👏 🙌 👏 🙌

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [May 22, 2020, 6:54pm UTC](https://in-thread.sonic-pi.net/t/oscjs-meets-sonic-pi-incoming-port-number/3859/5 "2020-05-22T18:54:28Z")

</div>

Glad you got there in the end. Enjoy using Sonic Pi via OSC!
