I am working on project that involves real time temperatures and have a device sending temps via get that is routed through the server and emitted to the socket. Then I want the server to connect to the original socket and emit the data to a new one that is being read by my client.
Here is my app.js
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(8080);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.route('/:temp')
.get(function (req, res){
var temp = req.params.temp;
res.end(temp);
io.sockets.on('connection', function(socket){
socket.emit('send temp', temp);
});
});
io.sockets.on('connection', function(socket){
socket.on('send temp', function(data){
console.log('connected to send temp');//this never shows in the console
io.sockets.emit('new temp', data);
});
});
The route code in app.js works fine. When I hit localhost:3000/test and change the client to connect to 'send temp' (rather then 'new temp') 'test' is output.
Here is the relevant section of my client
var socket = io.connect();
var $temp = $('#temp');
socket.on('new temp', function(data){
$temp.html("Temp: " + "<br/>"+data);
});
I am running node version 4.1.2, socket 1.3.7 and express 4.10.8.
I am wondering why I cannot connect to the original socket a second time. Or that may not even be my problem. I have studied the many 'chat' tutorials and searched for others using trying to do what I want to without any success.
Ultimately what I am trying to have happen is have a client hit /:temp over and over with a real-time reading and then have other clients get that data in real-time.
This is all still a little new to me so any help is appreciated.
Your code example registers a message handler on the server for the 'send temp' message. The client registers a message handler for the 'new temp' message.
The two (client and server) are then sitting there in a stalemate waiting for someone to first send a message, but (per the code you've disclosed) nobody ever does.
I don't really understand what the intent is for your code, but I see several issues.
First off, you do not want to install a listener for the connection event inside this code:
app.route('/:temp')
.get(function (req, res){
var temp = req.params.temp;
res.end(temp);
io.sockets.on('connection', function(socket){
socket.emit('send temp', temp);
});
});
Why would you only start listening for a connection event when you get a particular route handler. And, why add yet another event handler every time that route is hit. This code is just completely wrong. I don't know what you thought you were trying to achieve with it, but it's not the way to do things.
Second off, this code is waiting for the client to send a 'send temp' message and when it gets one, it attempts to broadcast that to all clients. But, the part of your client you disclose never sends a 'send temp' message.
io.sockets.on('connection', function(socket){
socket.on('send temp', function(data){
console.log('connected to send temp');//this never shows in the console
io.sockets.emit('new temp', data);
});
});
Third off, please describe exactly what you're trying to accomplish in words so we can better know what code to recommend in order to do that.
EDIT
Now that you've described the actual problem here:
Ultimately what I am trying to have happen is have a client hit /:temp
over and over with a real-time reading and then have other clients get
that data in real-time.
It is a little easier to recommend a solution:
On the server:
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(8080);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/:temp', function (req, res) {
var temp = req.params.temp;
res.end(temp);
// send this temperature to all connected clients
io.emit('new temp', temp);
});
On the client:
var socket = io.connect();
var $temp = $('#temp');
socket.on('new temp', function(data){
$temp.html("Temp: " + "<br/>"+data);
});
Related
I'm using Socket.io to do some communication between a phone and a web page, and I'm having issues with the server constantly dropping connections. I've seen a lot of other people posting about Socket.io where their connections drop after being idle for about 5 minutes or so, but this is a connection that has constant information being emitted back and forth, and the longest I've been able to maintain a connection is about 30 seconds.
The server side code I'm running is:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function(socket){
socket.on('create', function (room) {
socket.join(room);
});
socket.on('player-move', function(msg){
roomId = msg['room'];
move = msg['action'];
socket.broadcast.to(roomId).emit('player-move', move);
});
socket.on('disconnect', function() {
console.log('someone has disconnected');
});
});
http.listen(443, function(){
console.log('listening on *:443');
});
The client side is pretty basic emit events. Here's an example:
$( "#arrow-left" ).on( "touchstart", function(){
var senddata = { "room": roomId, "action": "d-l" };
socket.emit( "player-move", senddata );
});
Everything seems to be running fine until 5-30 seconds after everything starts and then the connection just drops. The connection usually starts up again, but after 5-10 seconds. Has anyone had similar issue occurring? Would this potentially be an issue with the server rather than the code? I've done similar things before on different servers with Socket.io and haven't had this kind of connection issue. Any help or direction to go in would be appreciated. Thanks!
I had the same problem with another library called ws and I tried to handle the disconnection method properly by deleting the specific client sending the disconnection request from a array of clients. I don't exactly know how, but it solved my problem.
clients = []
io.on('connection', function(socket){
clients.push(socket);
// above methods
socket.on('disconnect', function(){
console.log("disconnected");
deleteFromArray(clients, socket.id);
});
});
function deleteFromArray(arr, element) {
position = arr.indexOf(element);
arr.splice(position, 1);
}
I made a simple app with socket.io and node.js, it is a push notification service.
When a user connects to the websocket, sends his username (and the name of dashboard) and immediately joins to a room named as the user. The application listens for POST request, saves them and then emits a message to the user's room.
io.on('connection', function(socket) {
socket.on('userid', function(data) {
socket.join(data.userid+'-'+data.dashboard);
notification.find({userid: data.userid, to: data.dashboard, received_by_user: false}, function(err, notifs) {
socket.emit('get_notifications', notifs);
}
}
});
app.post('/notify', function(req, res, next) {
var notif_recv = new notification(req.body);
notif_recv.save(function (err, notif_recv) {
io.sockets.in(notif_recv.userid+'-'+notif_recv.dashboard).emit('new_notification', notif_recv);
});
res.send(200);
});
When I test it with node locally, it works fine, I send a POST to /notify and I can see the notification arriving at the dashboard. The problem is, when I test on an AppService on Azure, the client connects to the websocket and receives the first notifications (get_notifications event), but when I POST to /notify, the client doesn't receives anything! (no new_notification event, io.sockets.in(...).emit(...) doesnt seems to work on Azure).
For debugging purposes, I used console.log(...) to log at the server the returned value of functions socket.join(...) and io.sockets.in(...).emit(...), resulting that io.socket.in(...).emit(...) seems to return a io server without any channels and connections!
IIS could be messing with it? I tend to think that IIS have different processes for app.post('/notify'... and io.on('connection'... so, the socket I am referencing on app.post is DIFFERENT from I am joining the user in io.on('connection'.. (socket.join(...)).
Any tip when using socket.io rooms in Azure/IIS?
Thanks!
Did you try adding a 'disconnect' handler for debugging?
io.on('connection', function (socket) {
socket.on('disconnect', function(){ //add this part
console.log('Client disconnected');
});
socket.emit('get_notifications', "get_notifications_:"+new Date());
});
Also log at every emit and receive, so that you know when the client gets disconnected exactly. If its getting disconnected after the get_notifications event, make sure you are not sending a callback to the client or that the client is not expecting a callback from the server.
I notice some missing closing braces for the notification.find(..) and socket.on('userid'..). I hope that is only in the code that you pasted here.
To test the socket.io and POST issue on Azure App Services, I simplify the project and test on Azure.
The test server.js:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var bodyParser = require('body-parser')
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
server.listen(process.env.PORT ||3000, function(){
console.log('listening on *:3000');
});
app.get('/', function (req, res) {
res.sendfile(__dirname + '/client.html');
});
app.post('/notify', function(req, res, next) {
io.sockets.emit('new_notification', req.body);
res.send(200);
});
io.on('connection', function (socket) {
socket.emit('get_notifications', "get_notifications_:"+new Date());
});
The client content:
<body>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
$.post("/notify",{
msg:"notification from client"
});
socket.on('get_notifications',function(msg){
console.log(msg);
});
socket.on('new_notification',function(msg){
console.log(msg);
});
</script>
</body>
And this simple project works fine on Azure App Services.
So it could be some other part of your application which raise your issue.
You can check the Diagnostics Logs or leverage Visual Studio Team Services to troubleshooting the issue. Refer to the answer of How to run django manage.py command on Azure App Service for how the enable the Team Services extension on Azure App Services.
Any further concern, please feel free to let me know.
I just started learning NodeJS and I am trying to make a simple server-client project using Socket io.
What happens right now is that when I open localhost:8001, I don't see any logs inside the listener.sockets.on block.
var http = require('http');
var app = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('<h1>Hello!</h1>');
}).listen(8001);
var io = require('socket.io');
var listener = io.listen(app);
console.log("Sample Socket IO");
listener.sockets.on('connection', function (socket) {
console.log('a user connected');
socket.emit('connected', 'Welcome');
socket.on('disconnect', function () {
console.log('user disconnected');
});
});
It looks like the logging will occur when a connection happens. You setup a listening socket, so try to connect to it. Try 'telnet 127.0.0.1 8001' to connect.
The browser page needs to load the socket.io client code for one thing. That is the first thing missing that I can see. Look through the example here http://socket.io/get-started/chat/ and make sure you are following exactly at first and then make changes after you get that example working. Your server code looks a bit different from their example also.
I am working on realtime data visualization application using node.js, express and socket.io.
Requirement:
Have to emit the events based on the client request.
For example: If user enter the url as http://localhost:8080/pages socket.io should emit the topic pages to client and another user request for http://localhost:8080/locations socket should emit location to that particular user.
Code
var server = app.listen("8080");
var socket = require('socket.io');
var io = socket.listen(server);
var config = {};
io.on('connection', function (socket) {
config.socket = io.sockets.socket(socket.id);
socket.on('disconnect', function () {
console.log('socket.io is disconnected');
});
});
app.get('/*', function(req, res) {
var url = req.url;
var eventName = url.substring('/'.length);
//pages and locations
config.socket.volatile.emit(eventName, result);
});
Client Code:
//No problem in client code.Its working correctly.
Sample code as follows
socket.on('pages', function (result) {
console.log(result);
});
Problem:
It is emitting pages and locations to both the clients.
Any suggestion to overcome this problem.
I couldn't understand your approach on this, but because you said you're rendering different pages, It means you can serve different code, so what about doing it like this:
Server Side:
var server = app.listen("8080");
var socket = require('socket.io');
var io = socket.listen(server);
var config = {};
app.get('/pages', function(req, res) {
res.render('pages.html');
});
app.get('/locations', function(req, res) {
res.render('locations.html');
});
io.on('connection', function (socket) {
socket.on('pagesEvent', function(data){
socket.volatile.emit('pages', {your: 'data'});
});
socket.on('locationsEvent', function(data){
socket.volatile.emit('locations', {your: 'data'});
});
});
On Client side:
pages.html:
socket.on('connect', function(){
socket.emit('pagesEvent', {});
});
socket.on('pages', function(data){
// do stuff here
});
locations.html:
socket.on('connect', function(){
socket.emit('locationsEvent', {});
});
socket.on('locations', function(data){
// do stuff here
});
You are doing it wrong, WebSockets supposed to work same in both directions. Client emit event to Server, server emit back to Client/Subscribers.
The way you are doing things, seems like a way of implementing API, but for some reason you are trying to implement it with WebSockets, instead of XHR.
Right now I have the following code: foo
sIo.sockets.on('connection', function(socket){
socket.emit('hello', 'world');
});
I would like to be able to emit this when somebody opens a page from my routes, like this:
//app.js
app.get('/send', routes.index);
//routes.js
exports.index = function(req, res){
socket.emit('hello', 'world');
};
How can I achieve this? Thanks in advance
To send a socket message to all connected sockets, you can just call io.sockets.emit instead of socket.emit. There are a few ways to send messages using socket.io which I'll outline below.
// Send the message to all connected clients
io.sockets.emit('message', data);
// Send the message to only this client
socket.emit('message', data);
// Send the messages to all clients, except this one.
socket.broadcast.emit('message', data);
There is also a concept of rooms which you can use to segment your clients.
// Have a client join a room.
socket.join('room')
// Send a message to all users in a particular room
io.sockets.in('room').emit('message', data);
All of that covers how to send messages, but it's clear now you're asking about how to access the socket and / or io objects from inside a separate file. One options just has you pass those dependencies to the specified file. Your require line will end up looking something like this.
var routes = require('./routes')(io);
Where io is the socket.io object created from .listen. To handle that in your route file you'll have to change how you're defining your exports.
module.exports = function(io) {
return {
index: function(req, res) {
io.sockets.emit('hello', 'world');
res.send('hello world');
}
};
}
A cleaner implementation would have your routes expose events that your socket code can bind to. The following is untested, but should be very close.
var util = require("util"),
events = require("events");
function MyRoute() {
events.EventEmitter.call(this);
}
util.inherits(MyRoute, events.EventEmitter);
MyRoute.prototype.index = function(req, res) {
this.emit('index');
res.send('hello world');
}
module.exports = new MyRoute();
And then in your app.js file when you're binding express routes and socket.io.
app.get('/send', routes.index);
routes.on('index', function() {
io.sockets.emit('hello', 'world');
});
There are many other ways to accomplish this, but the best one depends on what you're trying to do. As I alluded to before, calling broadcasting to everyone is going to be far more simple than broadcasting to a particular user.