Pages: [1]   Go Down
Author Topic: Sending realtime data with node.js and socket.io  (Read 2005 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's my code:

Node:
Code:
var express = require('express')
, app = express()
, server = require('http').createServer(app)
  , io = require('socket.io').listen(server)
, dgram = require('dgram');

server.listen(5000);

var client = dgram.createSocket('udp4');
var message = new Buffer("100 101 102");

app.use(express.static(__dirname + '/'));

io.sockets.on('connection', function(socket) {
console.log("Server Connected");
socket.on('message', function(data) {
console.log(data);
client.send(message, 0, message.length, 8888, '192.168.1.1')
});
});

relevant html javascript:
Code:
<script src="/socket.io/socket.io.js"></script>
<script>
socket = io.connect('http://localhost');
$(window).click(function() {
sendData(100, 101, 102);
});
</script>

I have an ethernet shield that simply changes the color of an led. Seems to work fine when I run it on my machine, but if I try to run it on heroku or nodejitsu, it returns errors.  Heroku says it can't find Socket.io, and nodejitsu runs but never sends the data and crashes after a couple requests don't go through. Does anyone have experience with node.js and socket.io? I'm very new to it. Is there a better approach for realtime data transfer from a website to an ethernet arduino?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5852
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you using UDP? What OS are you using on the computer? I have some C code for Linux that works ok. If you are using Linux, let me know and I'll modify it to post here.

edit: I use an Apache server running the UDP code with a MySQL database linking the two.
« Last Edit: October 30, 2013, 01:47:52 pm by SurferTim » Logged

Tasmania - Australia
Offline Offline
Sr. Member
****
Karma: 14
Posts: 306
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm currently working on a similar project, well, I should say, continuing to work on.
My main aim is to provide next to real time remote monitoring, alarming and trending of a remote site and while I have it working well via traditional HTTP protocol I have been playing around with publishing from the Arduino to PubNub which clients can subscribe to.

I intially tried SSE, (Server Sent Events) but that doesn't work from my standard hosted site.
I have Nod.js on my mac but again I can't install it on my hosted site either.

I made a small test the other day where the Arduino publishes data to PubNub, works quite well.
The data from the Arduino to PubNub is still HTTP, but the cleint side is using server push technology.
I would like to see that it may be possible to send from Arduino to PubNub via UPD, but that is not available as yet.
The beaut thing with this topology that it is designed for fairly high throughput of data on a one to many relationship.
And it is just what I need for having alarm conditions being sent without polling.

Take a look at PubNub, especially the developers section, http://www.pubnub.com/developers

Would be interested to learn more about what it is you are wanting to do.
Presently working on new statistical gauges, all javascript, eeew.
____
Paul
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using Heroku for hosting as they recently added support for websockets. I'm on a Windows system. Right now the main goal is to just control a rgb led over a website. Similar to the phillips smart hue bulbs (https://www.meethuom/en-US). I need it to be quick and realtime, so when I drag my finger across an rgb spectrum it shows all of the colors that I hit on my way from point a to point b.
Logged

Pages: [1]   Go Up
Jump to: