Socket.io with Heroku - javascript

This works perfectly on my own computers server
var socket = io.connect('http://localhost:3000');
socket.of(userid).on('email', function (data) {
console.log("here")
});
however when I switch to heroku like
var socket = io.connect('http://staginggorkemnutrition.herokuapp.com');
socket.of(userid).on('email', function (data) {
console.log("here")
});
it seems like the event is never received, also I dont change anything on my server when I am changing from heroku to localhost
ps.
I have this in my server, I know I have to add this for heroku
global.io.configure(function () {
global.io.set("transports", ["xhr-polling"]);
global.io.set("polling duration", 10);
});
and my server side code when I emit the message looks like looks like:
exports.getFake = function(req, res){
global.io.of(global.id).emit('email' , email);
}

Web socket is experimental on Heroku.
Did you read this : https://devcenter.heroku.com/articles/heroku-labs-websockets ?
You may want to read this too : heroku multiple dyno socket.io

Related

How data arrives from a gps tracker by gprs to a node.js server?

I created an application to show the location of a gps in real time using gprs. This application uses node.js. I have already created all the methods and in tests it works correctly. I set up the gps to an external gps server and send the data correctly. Now I configured it with my server and happens is I have the socket.io with open listening but the data does not arrive. Could someone help me to see why the data does not arrive or if it is arriving because I cannot read it?
This is the code to listen:
var socket = io.connect('http://34.226.95.13:5050', { 'forceNew': true });
socket.on('data', function(data) {
console.log(data);
})
socket.on('connection', function (socket) {
socket.on('data', function (data) {
console.log(data.toString());
var location = JSON.stringify(data);
socket.emit('data', item);
});
});
Thanks
I was searching the internet a lot and found a way to receive the information only with the DATA parameter, but I went a little further and found that the server returned an answer like this: ##, imei: 359xxxxxxxxxxxx, A;
Then, I kept investigating and had to respond to the device with LOAD, after doing my own development I found a repository that has everything very well organized and is the best to use.
To install you need to enter the following command in the console:
npm install gpstracker
And next, create index.js and copy this code:
var gpstracker = require("gpstracker");
var server = gpstracker.create().listen(5050, function(){
console.log('listening your gps trackers on port', 5050);
});
server.trackers.on("connected", function(tracker){
console.log("tracker connected with imei:", tracker.imei);
tracker.on("help me", function(){
console.log(tracker.imei + " pressed the help button!!".red);
});
tracker.on("position", function(position){
console.log("tracker {" + tracker.imei + "}: lat",
position.lat, "lng", position.lng);
});
tracker.trackEvery(10).seconds();
});
Thanks

get data from mqtt broker -6lbr

I followed this tutorial :
Running the CC26xx Contiki Examples
but instead of using the cc26xx-demo I used the cc26xx-web-demo and successfully manged to get everything up and running and I can access the 6lbr web page, when I access the sensorTag page I see a mqtt configuration page as shown:
and if I click index in the sensorTag page (pic above) I get to see the data:
the question is , how can I write a simple nodejs js file that uses the mqtt broker information to grab all the sensorTag sensors data and save it in an local object.
I tried to do run this example but no luck
var mqtt = require('mqtt')
client = mqtt.createClient(1883, '192.168.1.109');
client.subscribe(what do I write here);
client.on('message', function(topic, message) { console.log(message); });
I don't know what I'm doing wrong
UPDATE:
mqtt configuration page :
javascript file :
and I run the js with node and listen on port 1883:
tcpdump seems to detect mqqt packets on 1883 port but I can't to seem to be able to console.log the sensor data when I run the js file with node ??
I went on the contiki wiki and came across this info
"You can also subscribe to topics and receive commands, but this will only work if you use "Org ID" != 'quickstart'. Thus, if you provide a different Org ID (do not forget the auth token!), the device will subscribe to:
iot-2/cmd/+/fmt/json"
does this mean that the topic to subscribe to is quickstart but even if that's so, I used '+/#' which subrcibes to all topics and still got nothing printing on the console ?
Hope this works for you:
var mqtt = require('mqtt');
var fs = require('fs');
var options = { port: PORT, host: HOST };/*user, password and others authentication if there.*/
var client = mqtt.connect('HOST', options);
client.on('connect', function ()
{
client.subscribe("topic, command or data");
client.publish("topic, command or data", data, function () {
});
});
client.on('error', function () { });
client.on('offline', function () { });
client.on('message', function (topic, message) {
console.log(message.toString());
});

Emit Events To Socket.IO Not Working From Server

i am trying to get a chat program to work using socket.io but it doesnt seem to work properly.
i am using a Node.js server and it seems to be running properly. i think this may have something to do with emitting to rooms.
the code i have on the client browser is:
<script>
var socket = io("https://localhost:3000/userChat");
socket.on('connect', function(){
socket.emit('initialiseConnection', "user1");
});
socket.on('messageIn', function(msg){
console.log(msg);
$('#messages').append($('<li>').text(msg.message));
});
</script>
so when the page loads, socket.io it connects to the server and emits the "initialiseConnection" event on the server with "user1". which is a new room specifically for that user.
on the server, the code i have handling "initialiseConnection" is:
socket.on("initialiseConnection", function(username){
socket.join(username);
console.log(username + " has joined.");
Message.find({recipient:username}, function (err, message){
console.log("messages for "+username+": "+message.length);
for (var x = 0; x < message.length; x++){
console.log(username+"'s message "+x);
console.log(message[x]);
socket.to(username).emit("messageIn", {"message":message[x]});
}
});
});
this code as you can see, creates and joins a room with the same name as the username. the looks in the database for any messages, and tries to emit those messages to the user. i log the message. there is definately a message and the username in the "socket.to()" method is also correctly shown in the logs. but the "socket.on('messageIn')" on the client browser doesnt seem to be picking up the event.
i have also tried putting:
setTimeout(function() {
socket.to(username).emit("messageIn", {"message":"test message"});
}, 5000);
immediately after the socket.join(), in case this was related to some backbround processing that needed to complete
can anyone see where i may have gone wrong on this?
Thanks.
--EDIT 1--------------------------------------
var https = require('https');
var express = require('express');
var app = express();
var server = https.createServer(https_options, app).listen(3000);
var io = require('socket.io')(server);
var userChat = io.of("/userChat");
userChat.on('connection', function(socket){
console.log('a user connected');
socket.on("initialiseConnection", function(username){
...
});
socket.on('disconnect', function(){
socket.leave(socket.room);
});
});
You need to change this:
socket.to(username).emit("messageIn", {"message":message[x]});
to this:
socket.emit("messageIn", {"message":message[x]});
A socket is the endpoint. When sending, you just send directly to the socket. You don't send to a user in a room? You just send to a socket. Since you already have the socket in your socket variable, that's what you send to.

Can't send message to only one specific room through node.js and socket.io

I have a problem that i don't seems to be able to solve it. I'm doing some kind of integration with remote system and my code is in iframe but that can't be important for this one i hope :).
I'm trying to send a message from server to specific room/client to begin session. First thing I do is when user log in, I emit message from client side with username.
CLIENT.JS
conn.on('connect', function () {
conn.emit('session', { username: 'some_username' });
}, false);
And on server side i get message and join socket to the room.
SERVER.JS
socket.on('session', function(session) {
socket.join(session.username);
});
I have another module that communicates with this server.js script through redis. So i have two more events in server.js
SERVER.JS
var userCreate = redis.createClient();
userCreate.subscribe("userCreate", "userCreate");
var userDestroy = redis.createClient();
userDestroy.subscribe("userDestroy", "userDestroy");
userCreate.on("message", function(channel, data) {
socket.to(JSON.parse(data).username).emit('beginSession', data);
});
userDestroy.on("message", function(channel, data) {
socket.to(JSON.parse(data).username).emit('endSession', data);
socket.leave(JSON.parse(data).username);
});
But when ever i try to emit message from server to client i broadcast message to everyone. What am I doing wrong?
Well, from the syntax point of view you are doing everything correct.
Didn't you forget to specify the userId property in the endSession?
userDestroy.on("message", function(channel, data) {
socket.to(JSON.parse(data).userId).emit('endSession', data);
socket.leave(JSON.parse(data).userId);
});
If that doesn't work - you should provide the contents of a data object

When actually socket is connected in socket.io?

I'm trying to understand, when socket is connected in socket.io. I'm using express and jade. To find out, I have something like this in my server.js file:
io.sockets.on('connection', function(socket) {
console.log('Socket is connected: ' + socket.id)
This message appears each time, when I open localhost in browser, and it's ok, as I understand - each opening localhost creates new connecting - new socket.
But sometimes localhost is not open and when I start my server in node, I get this message what makes me confused. Why is this happening ?
Second strange thing, I load my views via ajax :
socket.on('connect', function() {
socket.on('loadRooms', function(data) {
var active_users = data.active_users,
socket_id = data.socket_id;
$.ajax({
url: 'rooms'
}).done(function(data_view) {
$('body').html(data_view);
console.log(data.active_users);
});
and I also don't understand why after this I get another message in my console with new socket, seeing that I don't refresh my browser... Anyone could explain me that ?

Categories

Resources