n-riesco / ijavascript

IJavascript is a javascript kernel for the Jupyter notebook
Other
2.19k stars 185 forks source link

Simple export #21

Closed rgbkrk closed 9 years ago

rgbkrk commented 9 years ago

I'm hoping to use a common code base for the Jupyter messaging protocol, in my case for some alternative front end bits.

This sets up Message to be exported from jp.js (since that's all that comes out). I'm open to having more exposed but this is all I needed at the moment.

n-riesco commented 9 years ago

That's great! Would it help if I move the jp module into its own npm package?

Here are my thoughts about the pull request.

rgbkrk commented 9 years ago

What I ended up doing last night was

var Message = require('ijavascript/lib/jp.js');

I'm extremely new to packaging node application, so I'm willing to change this to any way you like it.

rgbkrk commented 9 years ago

Updated it to only export jp.js.

n-riesco commented 9 years ago

After some more thought, this is where I stand:

For those reasons, I've decided to move jp into its own npm package. I've renamed the module as jmp (Jupyter Messaging Protocol), because the jp is already taken. I've also changed the way to import class Message (so that I can export other symbols in the future).

To use the new package, you can replace:

var Message = require('ijavascript/lib/jp.js');

with:

var Message = require('jmp').Message;

Please, let me know if this change wouldn't work for you.

rgbkrk commented 9 years ago

Totally works for me!

I'm likely duplicating code that could be propagating converted messages, perhaps we can build up some other tooling that submits Message events?

/**
 * @class Snupyter
 * @classdesc Snoops on messages from a Jupyter kernel
 * @param {Object} connectionJSON Connection information provided by Jupyter
 */
function Snupyter(connectionJSON) {
    /**
     * Connection information provided by Jupyter
     * @member {Object}
     */
    this.connectionJSON = connectionJSON;

    /** IOPub socket
     * @member {module:zmq~Socket}
     */
     this.iopubSocket = zmq.socket("sub");
     this.iopubURL = formConnectionString(this.connectionJSON, "iopub");

     console.log('Connecting to ' + this.iopubURL);

     this.iopubSocket.connect(this.iopubURL);
     this.iopubSocket.subscribe('');

     this.iopubSocket.on("message", onIOPubMessage.bind(this));

     function onIOPubMessage() {
       var msg = new Message(
         arguments,
         this.connectionJSON.signature_scheme.slice("hmac-".length),
         this.connectionJSON.key
       );
       console.dir(msg);
     }

}

var snupyter = new Snupyter(config);

I'm doing this to create an Electron app that can display outputs from Jupyter kernels, starting with an HTML side car to a console:

screenshot 2015-04-30 08 37 07

n-riesco commented 9 years ago

I'm likely duplicating code that could be propagating converted messages,

I don't understand

perhaps we can build up some other tooling that submits Message events?

sorry I don't understand this either (:() Could you give me an example?

I'm going offline now. It's likely I won't be able to come back to you until tomorrow.

rgbkrk commented 9 years ago

Hey, that's ok. In particular, I'm thinking we could use a generic message handler to pass a message on to a callback or a stream:

     function onMessage() {
       var msg = new Message(
         arguments,
         this.connectionJSON.signature_scheme.slice("hmac-".length),
         this.connectionJSON.key
       );
       // pass msg on somehow
     }
n-riesco commented 9 years ago

This is what I came up over my tube journey:

/**
 * @class Snupyter
 * @classdesc Snoops on messages from a Jupyter kernel
 * @param {Object} connectionJSON Connection information provided by Jupyter
 */
function Snupyter(connectionJSON) {
    /**
     * Connection information provided by Jupyter
     * @member {Object}
     */
    this.connectionJSON = connectionJSON;

    /** IOPub socket
     * @member {module:jmp~Socket}
     */
     this.iopubSocket = new jmp.Socket(
          "sub",
           this.connectionJSON.signature_scheme.slice("hmac-".length),
           this.connectionJSON.key
     );
     this.iopubURL = formConnectionString(this.connectionJSON, "iopub");

     console.log('Connecting to ' + this.iopubURL);

     this.iopubSocket.connect(this.iopubURL);
     this.iopubSocket.subscribe('');

     this.iopubSocket.on("message", onIOPubMessage.bind(this));

     function onIOPubMessage(msg) {
       console.dir(msg);
     }

}

var snupyter = new Snupyter(config);
rgbkrk commented 9 years ago

Beautiful @n-riesco! That's just what I was hoping for. Thank you.

n-riesco commented 9 years ago

:) Thanks.

I've only tested that message listeners can be added, get the messages and can be removed. At some point in the nearish future, I will add some proper tests.