iam trying to connect php application with another nodejs application on heroku using socket.io
code using for connection on client (php app)
<script src="http://mynodeapp.herokuapp.com/socket.io/socket.io.js/"></script>
<script language="javascript" type="text/javascript">
var reguser_socket = io.connect('http://mynodeapp.herokuapp.com');
console.log("connected");
<script>
code using on server (nodejs app)
var http = require('http');
var port = process.env.PORT || 4000;
var app = http.createServer(handler).listen(port);
var io = require('socket.io').listen(app);
io.configure(function(){
io.set('log level', 1);
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
io.sockets.on('connection', function (socket) {
console.log("client has connection established ");
});
But while runnig this i get
GET http://mynodeapp.herokuapp.com/socket.io/1/?t=1384779309815
(Internal Server Error)
In order to use Web Sockets on Heroku, you need to run:
heroku labs:enable websockets
because it is only in public beta.
See here for a general overview on how to use it with Heroku:
https://devcenter.heroku.com/articles/node-websockets
This section of code works fine my connection was interrupted because i have another part of code cause this problem
Related
I'm using git to deploy my application not FTP. Also I've set at client side:
var socket = io({transports:['websocket']});
Also in server
var port = process.env.PORT || 3000;
...
io.on('connection', function(socket) { io.set('transports', ['websocket']);
console.log('new connection on socket.io');
socket.on('move', function(msg) {
socket.broadcast.emit('move', msg);
});
});
Websocket and 'Always On' is set on at azure and web.config does have:
But still emit to all sockets fails. I made simpler test version of my application. Deployed it again to heroku and had no problems.
Exact deployed application code can be seen here:
https://github.com/jmietola/testexpress
Please disable perMessageDeflate header when using socket.io ..
var io = require('socket.io')(server,{
perMessageDeflate :false
});
So I have a nodejs application which uses socket.io and expressjs
I use port '3000' for the express app and port '8080' for the socket app
Is it possible to use the same port for both these services?(express and socket.io)
When i want to connect to a socket from the client, I use the following code:
var socket = io('http://localhost:8080')
whats the right way of connecting to it( I see various ways in tutorials across the internet) and have no clue.
Is it possible to use the same port for both these services?(express and socket.io)
yes
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(8080); //or 3000
When i want to connect to a socket from the client, I use the following code:
At the front end :
include socket.io lib
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8080'); //or 3000
</script>
EDIT :
without express
var app = require('express').createServer();
var io = require('socket.io')(app);
app.listen(8080);
For more info socket.io
Yes, it is possible for both to use the same port. In fact, you should use the same port to make the client connect to your web socket when he sends a request. Socket.io docs has tutorial on how to connect with express.
Code from socket.io docs:
var app = require('express').createServer();
var io = require('socket.io')(app);
app.listen(80);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
If you want to connect socket with express-generator generated template, you this:
stackoverflow post
This function doesn't work in my app. I can't receive a message (socket.emit) from the server to the client (socket.on).
But i don't have this problem in the inverse (client to server).
I use cloud9 and the chat example from them works fine.
Here is my code for the server :
var http = require('http');
var path = require('path');
var async = require('async');
var socketio = require('socket.io');
var express = require('express');
var router = express();
var server = http.createServer(router);
var io = socketio.listen(server);
router.use(express.static(path.resolve(__dirname, 'client2')));
io.on('connection', function (socket) {
socket.emit('hello', 'i changed'); // !!!
});
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
var addr = server.address();
console.log("Chat server listening at", addr.address + ":" + addr.port);
});
and the code for the html page :
<html>
<head>
<title>test</title>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
socket.on('hello', function (msg) { // !!!
document.innerHTML = msg;
});
</script>
</head>
<body>
<p>normal page</p>
</body>
</html>
And there is no change...
edit : with console.log either.
But the message seems sent with the socket :
Debugger listening on port 15454
info - socket.io started
Chat server listening at 0.0.0.0:8080
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized 13XpSYGuXzMpMeyFsRfO
debug - setting request GET /socket.io/1/websocket/13XpSYGuXzMpMeyFsRfO
debug - set heartbeat interval for client 13XpSYGuXzMpMeyFsRfO
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"hello","args":["i changed"]}
Can you help me please ?
You need to change
var socket = io.connect();
by
var socket = io.connect('http://0.0.0.0:8080');
var socket = io.connect() <- this should specify the domain to which you are connecting.
So i did alot in this, trying to get it to work, it sorta did but i don't work like you do, i'm a beginner and not compatible with how you code but i've figured out that the problem is the port the io is using
var io = socketio.listen(server);
or where the html is getting the connection
<script src="/socket.io/socket.io.js"></script>
to me, the html is requesting a js file in it's own dir, but it shouldn't.
When i use socket.io, i put this in the server
var client = require("socket.io").listen(8080).sockets
The main part is the listen, it listens on the port 8080 and in my html i do
var socket = io.connect("http://127.0.0.1:8080");
Which works, i'm sorry for this, i am mostly sure this isn't an answer but i've tried, i hope you get it done, even if a year and 3 months have passed.
$(function(){
var socket = io();
socket.on('hello',function(data){
$('#lblmsg').text(data.message);
});
});
this is something I am working with. in the 4th line of my code, data is passed and data.message is retrieved. but you directly equated msg with the innerhtml, on the html page(in the block you commented \!!!)
see if you are still working on it!
I have a node.js project that works fine on my local machine running a node server. However when I deploy it to azure I can not connect to the websocket server. I heard somewhere that you may need to edit the web.config file to turn on web sockets, but I cant find that anywhere.
The server sets up a websocket like follows:
var http = require('http'),
port = process.env.port || 1337,
NodeSimpleRouter = require('node-simple-router'),
router = new NodeSimpleRouter(),
WebSocketServer = require('ws').Server,
wss = new WebSocketServer({port: 8080});
//create the server
http.createServer(router).listen(port);
console.log('Web server running on port ' + port);
and the client like this:
var socket = new WebSocket('ws://localhost:8080');
do I need to change any of these settings, such as the value of 'localhost'?
Azure can be a pain sometimes with websockets and node.js . Here is what I got to work using socket.io . I have my code deployed out as a cloud service. Be careful if you do VIP swaps from staging to prod because I have noticed it doesn't play nice with websockets.
app.set('port', process.env.PORT || 3000);
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(app.get('port'));
//Chat room
io.sockets.on('connection', function (socket) {
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
You can also override the websockets and force default of long-polling by using this:
//Over ride the Azure defaults
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
I am trying to connect two node.js servers using socket.io and socket.io-client. In both cases, I am using v0.9.16.
In my case, I call the servers the STREAM SERVER and the ACTIVITY SERVER where the activity server uses the socket.io-client module.
STREAM SERVER - (THE CODE ON THE SERVER THAT ACTS LIKE A SERVER)
var https = require('https');
var express = require('express');
var socket = require('socket.io');
var securePort = (process.env.LOCAL_HTTPS_PORT || 443);
var sslOptions = //CERTIFICATE
var socketIoConfiguration = //CONFIG VALUES
var app = express();
var server = https.createServer(sslOptions, app);
var io = socket.listen(server, socketIoConfiguration('activityToStream'));
io.sockets.on('connection', function (socket) {
console.log('Activity server connected to stream server.');
});
server.listen(securePort);
ACTIVITY SERVER - (THE CODE ON THE SERVER THAT ACTS LIKE THE CLIENT)
var socketClientModule = require('socket.io-client');
var streamConnectionServer = 'https://165.225.144.273:443';
var activityToStreamSocket = socketClientModule.connect(streamConnectionServer);
activityToStreamSocket.on('connect', function(socket){
console.log('Connected to Stream Server');
});
When I run this code, I don't get any message from either server. However, when I run this code from an HTML page served in Chrome, I see messages on the output of the Stream server:
<!DOCTYPE html>
<html>
<head>
<title>
Test
</title>
<script src="https://165.225.144.273/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('https://165.225.144.273');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</head>
<body>
Hello World
</body>
</html>
STREAM SERVER MESSAGES WHEN CLIENT RUN IN CHROME
debug: client authorized
info: handshake authorized UaUDRsA3ZBTgdsiLDCrl
debug: setting request GET /socket.io/1/websocket/UaUDRsA3ZBTgdsiLDCrl
debug: set heartbeat interval for client UaUDRsA3ZBTgdsiLDCrl
debug: client authorized for
debug: websocket writing 1::
So, it makes me think the issue is with my "ACTIVITY SERVER" running socket.io-client, but I cannot figure out why its not working at all (no error messages, etc.)
Use the same version of socket server and client
I had the same issue, but my solution was simple:
My code was wrong here:
var serverSocket = ioclient.connect('127.0.0.1:6000');
Then I wrote 'ws://' before '127.0.0.1', and it began to work.
var serverSocket = ioclient.connect('ws://127.0.0.1:6000');
That gave me a headache for hours.
I was able to get it to work using the following:
1) socket.io v 1.0.0-pre using this command
npm install git+https://github.com/LearnBoost/socket.io.git
2) scoket.io-client v 1.0.0-pre using this command
npm install git+https://github.com/LearnBoost/socket.io-client.git
3) I used an SSL certificate from a CA instead of self signed and then I used a DNS name instead of an IP address (see this on not using unsigned certificates with Socket.io)
Here's my server and client (server acting as client). Reference the Socket.io-client GitHub page.
SERVER CODE
var sslOptions = //CERTIFICATE
var app = require('express')(); // Express 3.x
var server = require('https').Server(sslOptions, app);
var io = require('socket.io')(server);
io.on('connection', function(socket) {
console.log('Connection from Activity Server');
for (var i=0; i<100000; i++) {
io.emit('event', i);
}
});
server.listen(443);
CLIENT CODE (SERVER ACTING AS CLIENT)
var now = Date.now();
var socket = require('socket.io-client')('https://example.com:443');
socket.on('connect', function(){
console.log('Connected to Stream Server');
socket.on('event', function(data){
console.log('Ping ' + data);
console.log(now + ' ' + Date.now());
});
});
Using this Socket.io code, I was able to get over 13,000 messages per second. This was done on two Joyent servers in the same data center (Memory 256 MB, CPUs 0.125 and bursting, Network Up to 10 Gbit/s).
When doing more of a "Ping Pong" style messaging, I was able to get about 650 messages per second.
Note the performance numbers are just a datapoint for reference, I didn't do any tweaking, heaving loading, etc.
I got the same situation but I fix this by switching the socketio-client version. I got socketio version 2.3. but I got socketio-client version 3.1. I changed the version of socketio client by using "npm install socket.io-client#2.3". in my client js file I use socket.io-client like that
const io = require('socket.io-client');
const socket = io('ws://localhost:3000',{
});