Node.js and HTML5 Audio - node.js

I'm trying to get my node.js server to server up audio files for the HTML5 audio tag. Currently I'm referencing audio files via an express staticProvider, but this will allow the sound to play once and then never again.
From what I've been able to gather in order for the sound to work properly with seeking or looping I need to provide the "Content-Range" header when my audio file is requested. I highly doubt that the express staticProvider is doing this for me, so it would seem that I need to serve the file up using custom code. Unfortunately I'm fairly new to node.js and things like serving files are still a little beyond me. Can anyone offer some advice on how I can provide the appropriate metadata for my audio files?
[EDIT] (Removed old server code since it won't do anyone any good!)
Sorry, I don't typically ask questions this broad, but I'm really at a loss as to where to start with this one. Any suggestions?
[SOLUTION]
So it appears that the solution is just to use updated software. I was trying express rather than the built-in connect HTTP middleware because I thought connect wasn't doing static file serving right. Turns out, I was just looking at the documentation for a newer version of connect while the version that comes with node is a bit older. Once I updated my connect library (I just used npm to install the latest, for those that are curious), the following worked beautifully:
var connect = require('connect');
var server = connect.createServer(
// If your server errors on this line, saying it doesn't know what
// "static" is you need to get the latest connect!
connect.static(__dirname + '/public')
);
server.listen(PORT);

The static serving logic is done by the underlying connect server (using its static middleware). There is code for 'Content-Range' headers (see here, about line 148), but these headers are only set when the underlying client (in your case, the browser's HTML5 implementation) sends the correct request headers.
Maybe this post from google groups can help you out. The topic is slightly different (video streaming), but i think the core of the problem is about the same.

Related

Loopback with i18n support

I'm trying to understand how to add i18n support to loopback which is built on top of express.js.
I've installed i18n-node and have read the express.js documentation. It looks like I don't want a global state of localization because I'll be serving HTTP responses. The i18n-node documentation recommends I attach it to the request object and gives an example.
My problem is that I can't find where/how to add this code into loopback. Everywhere I try to put the app.configuration function it says that method is undefined.
I have a feeling this is a middleware addition that I want to add to the middleware.json file in the routes phase. But I don't know how to do that.
Does anybody know a good way to internationalise a loopback app (server response messages, emails going out etc.)?
As per the documentation on their Github Page. I think the configuration code is best to keep in server/boot/ folder. As these are loaded as the server start and then, can be used by other rest endpoints. Don't forget to add JSON files for translations.
I don't think there are any other requirements as LoopBack is built on top of Express and most of their codes work same.

How can I sync the documents of shareJS with a fileSystem

I am trying to create a simple text editor that has Operational Transform multi tenant support and while it was reasonably easy to get the editor working and syncing across clients using shareJS, my problem is I would like to sync the shareJS doc's with a Folder structure on the Server side (this will eventually be a git repo)
I am completely new to sharejs and Operational Transforms and found the shareJS documentation a little tough to follow for more complex example.
Any suggestions on How might I approach this problem?
What I have tried to do is to implement a client on the server side that could get the entire doc text on update but (and this is the lack of experience I'm sure) the only way I can think to accomplice is to use the client api to cycle through all documents and write each to a file. But to me this sounds horribly inefficient. Can anyone point me to any resources that might help or offer some advice as to how I could approach this?
This is a bit late thought but you can still call getSnapshop method on the server side and dump that into a file on your file system. If it is not run locally you can create a tiny router with express on your local machine that listen for post request and you post the dumped file into the post request body on your sharejs server and then on your machine dump the post request body to a file, that should work.
Beware of security considerations if you use a auth system on your server.

How to access NodeJS data through cakePHP?

Dummy question here but I haven't found any answer on the web for now.
I'm working on a cakephp website, installed on a Raspberry Pi, which is supposed to be able to connect wireless-ly through a local network to an Arduino YUN, get its components list.
For example :
"A LED is connected on ports :
Input : 2,
Output : 6;
and is currently on"
And change the input value :
"turn the LED off"
I'm not supposed to reload the webpage to see the change occurring.
So I figured out I needed NodeJS to send/receive the informations with websockets but I don't know how to connect NodeJS -running on its own webserver- to cakephp.
I'm a complete rookie with NodeJS, just did a few tutorials earlier, so I'm stuck here.
Does anyone know how to deal with this ?
Thanks in advance,
There are a few different libraries you can use to connect to the node server.
http://elephant.io/
https://github.com/bergie/dnode-php
You can, of course, just fall back to sending http requests (curl) from your cakephp app to the node server.
I have a few discoveries to add that may help some people.
While searching more deeply on DNode and elephant.io I found a cakePHP plugin called cake_websocket which make use of socket.io.
Seems to me that it is an equivalent to elephant.io specialized in cakePHP (while elephant.io is 'just' PHP).
I hope it can help someone !
Instead of using any library.
You can try iframe tag with src to your 'node.js' view.html.
So, listen for events on view.html which are triggered from your 'cakephp' view.html
So, you will have cakephp->view.html talking to node.js->view.html which is connected to node.js->index.js(server)

How do I add a server side route that can call Meteor code?

I am trying to allow users to upload images to a site I have built with Meteor, which requires a server-side route I can POST data to. Is there a way to set up a route server side such that I can call Meteor code from it (for example, a route that I can call this.userId or Meteor.userId() from)?
Server side routing is on the roadmap, but not yet available the way you need it. (At that link, Server-side rendering is set for version 1.0).
In the interim, you can do some server side rendering with Tom Coleman's excellent meteor-router mrt package. It's unclear to me how much of this will make it into Meteor core.
More likely, however, for the file upload problem, this issue describes the problem, and it looks like people have had luck implementing imgur's xhr api or perhaps even better is this smart idea.
Hope this helps.

WebSockets noob working with Railo

I am admittedly a complete noob in all things server, Linux, and websockets. I finally managed to set up a VM running Apache, Tomcat, and Railo that I could connect to and serve up CFM pages, all the while learning UNIX command line navigation, server theory, etc, etc...
Here's my problem -- there is only one Railo websocket extension and it is super rinky-dink (I had to modify the CFC just to get the service to start) but I can't get a websocket connection up (I keep getting "unexpected code 200" in Google Chrome). There is minimal documentation, which is not helpful at all.
Basically, I am trying to do some prototyping for a future project that will use websockets. I like Railo for its speed, security, and excellent ability for very database heavy operations. I am interested in Node, but don't know how to get the same security and DB functionality out of Javascript as I can with CFML.
So I have a couple questions: what are my best options for WebSocket servers? Should I be trying to use Apache and/or Tomcat? People keep saying it's totally not worthwhile to have something like Node.js running the websockets portion and something else doing the heavy lifting behind it -- why is this? I'm more than happy writing WS handlers in whatever language if I can just get a nudge in the right direction, some excellent tutorials (I can't seem to find much in this department), or good feedback on how to, from the ground up, set up my Linux box to handle websockets -- and preferably how to handle both websockets and a robust language like Railo.
The Railo extension works fine for me.
What about submitting some test code so that we can debug it? Of course the websockets projects is very young and in full deployment. So feel free to fork and submit patches or suggestions.
You have plenty of options:
Railo Google Group
https://groups.google.com/forum/?fromgroups#!forum/railo
Github Extension Repository
submit a but in the Railo Jira bugtracker
The main problem of node.js is that it's mono-thread : you won't be able to do background tasks using it and local IO will block your server.
A solution I use is Go. It's very fast, has very good concurrency features and has integrated websocket and json libraries (sample : http://gary.beagledreams.com/page/go-websocket-chat.html). An efficient web application server is made in a few dozens lines of Go. You'll find that there is still much less documentation on internet than for java or even node.js through.
There are a few implementations of websockets in java but as I'm in the process of switching everything I had in java to Go I hadn't tested them. I know I use Google gson for the json encoding in java and it's very good.
The "unexpected code 200" is caused by Railo's web socket server sending an outdated response. They changed the web socket spec and Chrome uses the newer spec.
The problem seems to be caused by chrome & co implementing the new spec, "draft-ietf-hybi-thewebsocketprotocol-17". It requires the server to respond with "HTTP/1.1 101 Switching Protocols" rather than 200 OK.
The solution here would be to either update the Railo web socket extension yourself or use some other solution:
Here is a complete demo of a web socket chat server written in PHP.
http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/
I have used this myself to implement a real-time HTML chat served from an Arch Linux machine that I had lying around. Configuration consisted of simply setting up Apache and PHP then changing the IP address in index.html and in server.php to the external ip address of the server machine.
This flynsarmy demo includes a recent version of PHPWebSocket which is an open source web socket server written entirely in PHP and contained in a single file. The demo hooks into three callbacks: connect, message recieved, and disconnect.
The important thing to note, for me, was that the web socket protocol supports text only, not binary so while extending it for my own chat app I had to implement my own commands to help control the server. Commands in my case looked like this:
!kickusers: username, another_username, a_third_username
My server code would check the first character of all messages for a '!' and if present would treat it as a command. Then I slice up the string to get the command "kickusers" and a list of users to kick. Then I call the appropriate kick function and pass it the array of usernames.
Since my scenario was a chat client this meant that the user could literally type this command into chat and the server would accept and respond to it.
The way all this is deployed on my server is like so:
I have Apache serve the index.html page when the user goes to that location on my server in their browser. The only purpose Apache plays here is to give index.html to the client who requested it.
The index.html page contains html to display the chat and javascript to send and recieve chat to/from the server. Basically, index.html is simply a chat client written in HTML and Javascript and it runs in the browser.
I run server.php via ssh on the server to start up the WEB SOCKET server (totally separate from Apache) which just sits there and handles chat stuff like echoing text to the other connected clients etc.
Though the Arch wiki on installing Apache and PHP is specific to Arch in the way that you install the Apache and PHP packages the sections on configuring Apache and PHP apply to all. I'll save you the google query and give you the link here if you like: https://wiki.archlinux.org/index.php/LAMP
As for prototyping, the reason I gave the link to Flynsarmy's chat demo is because his comments are helpful, he wrote a blog about it, and it comes as a very simple yet complete example of how to do something with web sockets in php.

Resources