POST pusher/auth 404 (Not Found) - javascript

I'm trying to establish a private channel using Pusher on a local node.js server. For some reason, I can't get my auth endpoint to play nice and I keep getting a 404 error.
At first I thought it was an issue with how I was defining my endpoint in relation to the location of the local server, but I don't think that's a problem. More likely, my noobiness with server-client-api communication means I'm missing some big piece.
I've looked through the authentication docs on Pusher and literally every SO thread I could find, but to no avail.
I've got Node installed and the server running, and Pusher recognizes that a connection is made, I'm just failing at the authentication.
Any help at all would be mightily appreciated.
Here's the client-side JS that is called when a button is clicked over at index.html:
In client.js:
function startGame(){
var nameinput = prompt("Give your game a name","My Game");
if (nameinput !== null) {
var initialsinput = prompt("What are your initials?", "MG");
if (initialsinput !== null) {
var pusher = new Pusher(key);
Pusher.channel_auth_endpoint = 'http://localhost:8080/pusher/auth.js';
var channel = pusher.subscribe("private-"+gamename);
joined = 'client-opponent_joined'+gamename;
channel.bind('client-opponent_joined'+gamename, function(data) {
OnLikeDonkeyKong(data.nameinput,data.initialsinput);
});
}
else {alert("I need your initials.");}
}
else {alert ("I need a game name.");}
}
Then, over in /pusher/auth.js:
var express = require( 'express' );
var Pusher = require( 'pusher' );
var app = express( express.logger() );
app.use( express.bodyParser() );
var pusher = new Pusher( { appId: 'xxx', key: 'xxx', secret: 'xxx' } );
app.post( '/pusher/auth.js', function( req, res ) {
var socketId = req.body.socket_id;
var channel = req.body.channel_name;
var auth = pusher.authenticate( socketId, channel );
res.send( auth );
} );
var port = process.env.PORT || 8080;
app.listen( port );
Finally, here's the error I'm getting:
POST http://localhost:8080/pusher/auth.js 404 (Not Found)

http://localhost:8080/pusher/auth.js
This url is not exist on server. Check the location of auth.js again.
From Pusher document (link)
authEndpoint (String)
Endpoint on your server that will return the authentication signature
needed for private and presence channels. Defaults to '/pusher/auth'.
So you need to create your authentication endpoint on your server and provide a link to it while setup Pusher instance to authenticate.

Related

WebSocket connection to 'ws://(server ip)/socket.io/?EIO=4&transport=websocket&sid=52ViP_GqPGSr4DkqAAAN' failed:

I have been trying to figure out this error for a few days. When I try to emit a socket from the client side (JS) to the server (Python - flask) I get the error of connection failed. However, this error isn't always consistent. If the error does happen, it will normally happen on the first time I try to emit the socket and it normally works after that. Also, I am having some trouble with mobile devices receiving events as well. I also have an nginx reverse proxy on my server as well.
When I run this on local host I do not get the error of WebSocket connection failing.
Client side:
function formsubmitted(){
var name = document.getElementById("startexampleInputEmail1").value
var pin = document.getElementById("startexampleInputGamePin").value
var predictions = document.getElementById("startexampleInputPredictions").value
var joinmsg = document.getElementById("startexampleInputJoinMsg").value
socket.emit("AccountCreation",[socket.id,name,pin,predictions,joinmsg])
}
Server side:
#socketio.on("AccountCreation")
def handle_new_acc(data):
sid = data[0]
name = data[1]
pin = data[2]
predictions = data[3]
joinmsg = data[4]
canCreateAcc = True
gameExists = False
for user in User.query.all():
if user.name == name:
canCreateAcc = False
emit("UsernameTaken",to=sid)
return
for game in Game.query.all():
if game.pin == pin:
gameExists = True
if gameExists == False:
emit("GameNotFound",to=sid)
if gameExists == True and canCreateAcc == True:
usertoadd = User(name,pin,predictions,joinmsg,1,"0 seconds","0 seconds",0,"")
db.session.add(usertoadd)
db.session.commit()
game = Game.query.filter_by(pin=pin).first()
player_list = json.loads(game.players)
player_list.append(name)
game.players = json.dumps(player_list)
db.session.commit()
emit("AccountReady",[name,pin,predictions,joinmsg],to=sid)
There may be a problem with your nginx configuration. Just recently I had a similar issue (although the connections failed all the time, not just sometimes), see if some of it helps: Websocket failed: flask app with nginx and gunicorn, react frontend with socket.io

Trying to capture Client IP Address for already running server in localhost, but node.js conflicts with existing server

I am trying to capture client ipaddress for traffic visiting my localhost:8080. I am using the following modules and the node.js application looks like this
var connect = require('connect');
var http = require('http');
var net = require('net');
var express = require('express');
var app = express();
var app = connect();
// require request-ip and register it as middleware
var requestIp = require('request-ip');
// you can override which attirbute the ip will be set on by
// passing in an options object with an attributeName
app.use(requestIp.mw({ attributeName : 'myCustomAttributeName' }))
// respond to all requests
app.use(function(req, res) {
// use our custom attributeName that we registered in the middleware
var ip = req.myCustomAttributeName;
console.log(ip);
fs.appendFile('iplist.csv', ip, 'utf8', function (err) {
if (err) {
console.log('Some error occured - file either not saved or corrupted file saved');
} else{
console.log('It\'s saved!');
}
});
// https://nodejs.org/api/net.html#net_net_isip_input
// var ipType = net.isIP(ip); // returns 0 for invalid, 4 for IPv4, and 6 for IPv6
// res.end('IP address is ' + ip + ' and is of type IPv' + ipType + '\n');
});
//create node.js http server and listen on port
app.listen(8080);
Is there any way I can listen to the already existing server without creating my own, hence avoiding the conflict of two servers fighting for the same port. I am new to node.js. Any help will be great. Thank you!

Using Socket.IO from Node.js to connect to external server

Background: I have a node.js server running on my localhost (call this Server A); and an external server running node.js at https://example.net:3000 (call this Server B). I do not control or have access to Server B (it is a dashboard site for an IoT device in my home), but I need to connect to is using socket.io and emit a specific message.
I can connect to it easily from a flat javascript file (client-side), but need it running server side (ultimate goal is to make it into something I can call with an HTTP request); and examples such as How to connect two node.js servers with websockets? suggest I should be able to use socket.io-client from node.js with nearly the same code to achieve the same results. But when I run the code from node.js, I cannot connect to the socket.
Below is the code that works successfully in flat javascript file. I know it works because I see 'socket connect' in the console, and I can also test for the the socket emit at the end.
var myemail = "email#gmail.com";
var device_id = '12345';
// Create SocketIO instance, connect
var socket = io.connect('https://example.net:3000');
socket.on('connect', function(){
try {
console.log('socket connect');
socket.emit('configure', {email:myemail, deviceid:device_id});
} catch(e) {
console.log(e);
}
});
socket.emit("/" + device_id, "45678");
...and below is the code I cannot get to work when running from my node.js instance. I'd expect a message 'socket connect' in the command line log and get nothing.
var express=require('express');
var http=require('http');
var app=express();
var server = http.createServer(app);
//Variables
var myemail = "email#gmail.com";
var device_id = '12345';
var io = require('socket.io-client');
var socket = io.connect('https://example.net:3000');
//Connect listener
socket.on('connect', function(){
try {
console.log('socket connect');
socket.emit('configure', {email:myemail, deviceid:device_id});
} catch(e) {
console.log(e);
}
});
socket.emit("/" + device_id, "45678");
Any ideas?
UPDATE
Ran debug utility, results included as linked image below. Key thing I see is that engine.io tries to do an xhr poll, and gets a 503 response back from the server. (Obviously not a true 'temporary error' with the server as again, this all works from running client-side js in chrome).
debugging output image link
Solved this - issue was that the server I was connecting to required use of https, so I needed to add
{secure: true, rejectUnauthorized: false}
after the url to connect to.
Full working example:
const myemail = email#email.com;
const device_id = 12345;
io = require('socket.io-client');
var socket = io.connect('https://server.net:3000',{secure: true, rejectUnauthorized: false});
function doStuff(){
//Listener
socket.on('connect', function(){
try {
console.log('socket connect');
socket.emit('configure', {email:myemail, deviceid:device_id});
} catch(e) {
console.log(e);
}
});
socket.emit("/" + device_id, "003021");
}
doStuff();
I think the line causing the issue is :
var socket = io.connect('https://example.net:3000');
I managed to make a working example using this code :
const myemail = "email#gmail.com";
const device_id = '12345';
var socket = require('socket.io-client')('https://example.net:3000');
socket.on('connect', function(){
try{
console.log('socket connect');
socket.emit('configure', {email:myemail, deviceid:device_id});
}catch(e){ console.log(e); }
});

Socket IO - Socket.emit is sent to all clients

I am creating a game using socket io. A player connects like this:
var playerName = document.getElementById("name").value;
socket.emit('setup player', {
name : playerName
});
Then on the server, the player is setup and his information is sent back to the client:
function onSetupPlayer(data) {
...
var newPlayer = new Player(x, y, color, data.name,
this.id, scale);
socket.emit('setup game', {
localPlayer : newPlayer
});
...
sockets[this.id] = socket;
}
The following call:
socket.emit('setup game', {
localPlayer : newPlayer
});
Should send the setup data only back to the client that requested the setup to be done originally. However the setup call gets send to everyone in the lobby.
Could this have anything to do with the fact that I am using localhost to test it? I am also testing it on the same machine by using different tabs. If this is what is causing the issue, is there a way to resolve it? Since this is pretty annoying when testing my game.
EDIT:
Initialization:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var socket = require('socket.io')(http);
var path = require('path');
var io = require('socket.io')(80);
...
var setEventHandlers = function() {
socket.sockets.on("connection", onSocketConnection);
};
Listening for connection:
function onSocketConnection(client) {
...
client.on("setup player", onSetupPlayer);
...
};
And on the client side I have this:
var setEventHandlers = function() {
socket.on("setup game", onSetupGame);
...
}
socket.emit send event to everyone excepts this. To send data back to this user try
io.to(socket).emit()

Why does my websocket close after a few minutes?

I'm using ws with node.js on the server side and the regular WebSocket API on the client side. Opening the connection and messaging a few times back and forth works fine. But the socket always closes after a minute or two. Aren't they supposed to persist? Am I doing something wrong?
My server is node.js hosted on heroku. I just tested locally again using foreman start (the heroku tool to run the server locally) and the socket doesn't close unexpectedly at all, so perhaps it's a misconfiguration on heroku. Anyway, here's a relevant code sample with a few functions omitted for brevity.
I'm testing the application in Chrome on OSX Yosemite but have seen the same behavior in Chrome on Windows 7 when running against production environment.
server:
// Client <-> Host Protocol functions. Move to a different file so that they can be shared.
var C2H_SIGNAL_TYPE_REGISTER = "register";
var H2C_SIGNAL_WELCOME = "welcome";
var H2C_SIGNAL_TYPE_ERROR = "error";
var H2C_SIGNAL_TYPE_PEER_ADDED = "peer_joined";
var H2C_SIGNAL_TYPE_PEER_LEFT = "peer_left";
// Update channel endpoint names.
var UPDATE_ENDPOINT_PEERS = "/peers";
// Create a signal message with all asociated default properties.
// Signal senders should create this object and update it accordingly when
// building a signal message to send to a peer.
function createHostMsg(type)
{
var msg = { signalType: type };
if ( type == H2C_SIGNAL_WELCOME ) {
// Since we're sending a welcome message, we need to provide a list
// of currently connected clients.
msg.peers = {};
for ( var addr in clients ) {
console.log("addr " + addr);
var c = clients[addr].description;
if ( c && c.id ) {
msg.peers[c.id] = c;
}
}
}
return msg;
}
// require modules.
var express = require('express');
var http = require('http');
var bodyParser = require('body-parser');
var multer = require('multer');
// Tracks connected peers.
var clients = { };
// 1. Configure the application context settings.
var app = express();
app.enable('trust proxy');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json()); // parse json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(multer()); // for parsing multipart/form-data
// a. configure http routers. these will handle requests coming from app.
app.set('port', (process.env.PORT || 5000));
app.get('/app', peerApp);
app.get('/script/:name', publicScriptRouter);
// 2. Create the http server itself, passing app to be the request handler.
// app will handle routing and multiplexing of incoming requests to different
// route middleware handlers.
var http = require('http');
var WebSocketServer = require("ws").Server
var httpServer = http.createServer(app);
httpServer.listen( app.get('port') );
// 3. Create one of these for all socket endpoints.
var wss = new WebSocketServer( { server: httpServer, path: UPDATE_ENDPOINT_PEERS } );
wss.on("connection", function(webSocket) {
// 1. Associate the socket with the remote address it came from.
var remoteAddress = webSocket._socket.remoteAddress;
var remotePort = webSocket._socket.remotePort;
var clientConnID = remoteAddress + ":" + remotePort;
var exists = clients[clientConnID] != null;
if ( exists ) {
console.log("socket server connection: associating new connection from %s with registered peer.", clientConnID);
clients[clientConnID].socket = webSocket;
} else {
console.log("socket server connection: associating new connection from %s with unregistered peer.", clientConnID);
clients[clientConnID] = { description: null, socket: webSocket };
}
// 2. Hook up handlers for communication over this particular socket.
webSocket.on("message", function(data, flags) {
processMessage(webSocket, data, flags);
});
webSocket.on("close", function() {
// Praise satin for closures!!
removePeer(clientConnID);
});
});
// Transduce the message and handle it accordingly.
function processMessage(socket, data, flags)
{
var msg = JSON.parse(data);
if ( !msg.signalType ) {
var msg = createHostMsg( H2C_SIGNAL_TYPE_ERROR );
msg.errStr = "message_malformed";
socket.send( JSON.stringify( msg ) );
} else if ( msg.signalType == C2H_SIGNAL_TYPE_REGISTER ) {
handleRegistration(socket, msg);
}
}
client:
function initSignalChannel()
{
rtcPeer.channel = new WebSocket( location.origin.replace(/^http/, 'ws') + "/peers" );
rtcPeer.channel.onmessage = updateChannelMessage;
rtcPeer.channel.onopen = function(event) {
console.log("remote socket opened");
}
rtcPeer.channel.onclose = function(event) {
console.log("host closed remote socket.");
}
}
function updateChannelMessage(event) {
var msgObj = JSON.parse(event.data);
if ( !msgObj || !msgObj.signalType ) {
console.log("updateChannelMessage: malformed response!! %o", msgObj );
} else if ( msgObj.signalType == "welcome" ) {
console.log("updateChannelMessage: received welcome from host.");
handleWelcome(msgObj);
} else if ( msgObj.signalType == "peer_joined" ) {
console.log("updateChannelMessage: received peer_joined from host.");
if ( msgObj.peer.id == rtcPeer.description.id ) {
console.log("updateChannelMessage: peer_joined: received notification that I've been added to the room. " + msgObj.peer.id);
console.log(msgObj);
} else {
console.log("updateChannelMessage: peer_joined: peer %s is now online.", msgObj.peer.id);
console.log(msgObj);
addRemotePeer( msgObj.peer );
}
}
}
function addRemotePeer(peerObj)
{
remotePeers[peerObj.id] = peerObj;
var ui = createPeerUIObj(peerObj);
$("#connectedPeerList").append( ui );
}
function createPeerUIObj(peerObj)
{
var ui = null;
if ( peerObj ) {
ui = $("<li></li>");
var a = $("<a></a>");
a.append("peer " + peerObj.id);
ui.append(a);
ui.click(function(event) { console.log("clicked");});
}
return ui;
}
function handleWelcome(msgObj)
{
if ( msgObj.id ) {
console.log("updateChannelMessage: welcome: received id from host. " + msgObj.id);
console.log(msgObj);
rtcPeer.description.id = msgObj.id;
for ( var p in msgObj.peers ) {
addRemotePeer(msgObj.peers[p]);
}
} else {
console.log("updateChannelMessage: malformed response. no id.");
}
}
Thanks for the comments everyone. It turns out that jfriend00 had the right answer, I just didn't realize that the hosting service I was using wouldn't allow for the connection to be kept open.
From the below forum posting, the solution is
you'll need to make your clients ping the server periodically to keep the socket alive.
Not the most ideal situation, but indeed doable. Thanks for pointing me in the right direction.

Categories