Can I set up socket.io chat on heroku? - sockets

I have a simple socket.io chat application which I've uploaded to one of the new Heroku 'cedar' stacks.
Now I almost have everything working but I've hit one stumbling block. On my localhost, I open a connection to the socket server from the client with:
// lots of HTML omitted
socket = new io.Socket('localhost', {port: 8888});
But on Heroku, I obviously must substitute something else in for these values.
I can get the port from the process object on the server like so:
port = process.env.PORT || 8888
and pass that to the view.
But what do I substitute for 'localhost'?

The correct way according the article on heroku is:
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
socket = new io.Socket();
This ensures that io.Socket won't try to use WebSockets.

I was able to get Socket.IO v0.8 to work on Heroku Cedar by doing the following:
Within the Express app (in CoffeeScript in my case):
app = express.createServer();
socket = require("socket.io")
...
io = socket.listen(app);
io.configure () ->
io.set("transports", ["xhr-polling"])
io.set("polling duration", 10)
io.sockets.on('connection', (socket) ->
socket.on('myaction', (data) ->
...
socket.emit('result', {myData: data})
### The port setting is needed by Heroku or your app won't start
port = process.env.PORT || 3000;
app.listen(port);
And within the front-facing Javascript of your application:
var socket = io.connect(window.location.hostname);
function sendSocketRequest() {
socket.emit('myaction', $("#some_field").val());
}
socket.on('result', function(data) {
console.log(data);
}
Helpful links:
Heroku Node help
Heroku Socket.IO help

This has now changed as of Oct 2013, heroku have added websocket support:
https://devcenter.heroku.com/articles/node-websockets
Use:
heroku labs:enable websockets
To enable websockets and dont forget to remove:
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});

After trying every combination under the sun I finally just left it blank. Lo and behold that works perfectly. You don't even need the port.
socket = new io.Socket();

I was also having this problem on heroku. I was able to make it work using the hostname "myapp.herokuapp.com" (or simply window.location.hostname, to work both local and in production) and setting the port to 80. I'm using SocketIO 0.6.0.

Wouldn't you just put your actual hostname?

2011-06-25T21:41:31+00:00 heroku[router]: Error H13 (Connection closed without response) -> GET appxxxx.herokuapp.com/socket.io/1/websocket/4fd434d5caad5028b1af690599f4ca8e dyno=web.1 queue= wait= service= status=503 bytes=
Does this maybe mean the heroku router infront of the app is not configured to handle web socket traffic?
[update]
It would appear as of 6/22/2011 the answer is yes... heroku does not support socket.io see this post: http://blog.heroku.com/archives/2011/6/22/the_new_heroku_2_node_js_new_http_routing_capabilities/

Related

Flutter Socket.IO cannot connect to server, but browser works fine

Was going to use Socket.IO for authorizing in a flutter application, but it wont work on anything other than localhost:
String serverURL = "https://example.awsapprunner.com";
IO.Socket socket = IO.io('$serverURL/client', <String, dynamic>{'transports': ['websocket']});
checkUserExists(String phoneNum, context) {
socket.connect();
socket.emit('login', phoneNum);
socket.on('login_error', (res)=> {
log("new user"),
});
socket.on('session-started',(sid)=>{
log("old user!"),
});
}
The app won't even connect to the socket so anything after socket.connent() never runs.
Here's what I have tried
'autoConnect': true
replacing 'https' with 'wss' or 'ws'
removing 'https://' altogether
Different Cloud Services (AWS, GC, Azure)
local server (works and connects perfectly fine)
connecting to the server via browser (also works perfectly fine)

Testing feathers socket

I have hardly done anything with feathers sockets so far and therefore I need your help.
I have written a test which tests the functionality of my websocket to move users into the right channels. To do this I created two socketIo-clients in my test and let them connect to the websocket with the help of my own helpers, which also do the authentication when the connection is established.
If I run the test on its own, it will work fine. But once I run all the tests together, the test no longer works, because the SocketIo-Clients can not established any connection. This can be noticed, because no connection event is triggered at the server.
In my Test I do:
before((done) => {
server = app.listen(app.get('port'), done);
socketUrl = 'ws://localhost:5555}/';
});
socket = io(socketUrl);
and
after(async () => {
await server.close();
});
I found out, that any test, which is also doing server = app.listen(app.get('port'), done); before the socket test, will cause the socket test to fail. Can it be possible, that await server.close(); does not really close the http-server and/or the ws-server?

I can't get the client script to connect to the localhost server, the TCP connection never happens

I wrote a simple client side program that creates a socket using
CFSteamCreatePairWithSocketToHost function
and connects to the server that runs on the local host on port 8080. It creates the socket just fine but it never connects to the server. I wrote the server in C. It didn't work and gave me a
kCFErrorDomainCFNetwork error 72000
and the only information that relays is that apparently the TCP connection couldn't be made don't know why though. So I tried to write the client side script in C too and added it to my Swift project bridging header and all but it still doesn't connect. It creates the socket just fine but it fails to connect to the server and I have no idea why.
But the same C client script worked when I compiled it using clang and ran it but didn't connect when I ran it with my swift project in Xcode. Is my mac blocking the libraries from making a TCP connection or something?
I don't even know what to search for. The only thing I found was an issue on a Github library called starscream which had the same errors I had and I'm not even using that library and the reply there was "the only thing we can discern from this error is that the TCP connection was unsuccessful".
Here's the code I used to connect to the server using Swift 4. The server is running on port 8080 on localhost.
class client:NSObject {
var inputstream = InputStream!
var outputstream = OutputStream!
func setupNetworkCom() {
var readstream = Unmanaged<CFReadStream>?
var writestream = Unmanaged<CFWriteStream>?
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, "localhost" as CFString, 8080, &readstream, &writestream)
inputstream = readstream!.takeRetainedValue()
outputstream = writestream!.takeReatainedValue()
inputstream.schedule(in: .current, forMode: .common)
outputstream.schedule(in: .current, forMode: .common)
inputstream.open()
outputstream.open()
}
}
I've also tried replacing "localhost" with "127.0.0.1" which is the IP I specified for the server to run on but it still doesn't work.
click on your project settings and go to capabilities there you'll see the app sandbox. make sure it's turned on and then enable incoming connections and outgoing connections.

Frisby.js Error: tunneling socket could not be established

I am trying to test an REST API on my local machine using frisby.js . It throws the following error.
Error: tunneling socket could not be established.
The machine address is something like 'https://machine_name:8443'
It seems that you are behind a proxy. Then, to make your frisby test working, you need to apply proxy configuration as follows:
var frisby = require('frisby');
frisby.globalSetup({
request: {
proxy: 'http://xx.xx.xx.xx:yyyy' // Provide proxy info (host, port) here
}
});
frisby.create('Your spec description here')
.get('https://machine_name:8443')
.expectStatus(200)
.toss();
Note also that you are using HTTPS protocol. Then you may find useful my answer in this post in case you have problems with SSL certificates

Get data from an EventMachine server connection?

I'm attempting to create a Sinatra server that will return statistics about an EventMachine server. That is, I'm running:
EventMachine.run do
server = EventMachine.start_server 'localhost', 3333, MyApp
dispatch = Rack::Builder.app do
map '/' do
run MySinatraApp
end
end
Rack::Server.start({
app: dispatch,
server: 'thin',
Host: '0.0.0.0',
Port: '1111'
})
end
My goal is to figure out information on that running server started by start_server, such as connection_count. Is there any way to do this?
As far as I know there is no in-built way to do this (hopefully someone disproves me),
you can keep a counter in MyApp and +1 on connect and -1 on unbind for same effect.
Why? Why not just have the EM server provide a /info endpoint or something that returns a dump of the information you need? Why do you need the second server? If you really want a second server then it could just be a simple sinatra app that makes a HTTP request to /info and returns the results.
For connection_count it looks like there is a EM.connection_count you can call. You can see it here.