Node.js restify with socket.io - node.js

Is it possible to run socket.io & restify on the same port like express & socket.io?
I did just like this but it didn't work
# server.coffee
restify = require 'restify'
socket = require 'socket.io'
server = restify.createServer()
io = socket.listen server
server.listen 1337
when I try to connect to socket.io:
GET http://localhost:1337/socket.io/socket.io.js 404 (Not Found)

As suggested here by #jtomasrl and #zacheryph, this worked for me:
var server = restify.createServer();
var io = socketio.listen(server.server); //Note server.server instead of just server

Since this is the first google hit for "restify socket.io" I'm posting a new answer. This works fine now as documented at http://restify.com/docs/home/#socketio

Apparently, using socket.io with restify is not possible yet: https://github.com/mcavage/node-restify/issues/230

Related

How to get THREE.JS project to work with PHP not just HTML? [duplicate]

Ok I've been playing around with nodejs, expressjs and socket.io to create some applications. But now im coming to the stage where i want to take things a bit further.
I have noticed a few node apps using PHP for twitter auth on their client side. When I attempt to rename my client.html file to client.php and restart the server it throws up a blank page with this
Cannot GET /
How do would serve php files or would i do twitter auto using js?
This is my NodeJS server.js
var http = require('http'),
express = require('express');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
app.listen(1234);
console.log("server started on port :1234");
As already noted, node.js itself won't interpret php files for you. Available options are:
have nginx in front of node and reverse proxy all request you want to process in node from it
proxy php requests from node to php-capable server
spawn php process on each request in node
make fastcgi link to php-fastcgi using node fastcgi protocol parser
Uh, skip PHP entirely and integrate everyauth into your app.
PHP-EXPRESS should do. Just install the package and follow the docs.
https://www.npmjs.com/package/php-express

How to connect the socket.io in node.js (typescript )?

GET http://localhost:3001/socket.io/?EIO=4&transport=polling&t=NX0bUXk 404 (Not Found)
getting this type of error
actually from angular, the socket.io is connection to node which is written in ts
In node to create server
const server = http.createServer(app);
const io = new Server(server).listen(server);
initialize(io);
i'm using this lines to create the server and use the socketUrl in angular environment file as
http://localhost:3001/

Node - Attaching to server / websockets

I am running a vue application (VUE-CLI 3) on Node. In dev mode, I run 'npm run serve' and the application is brought up and works as expected.
I would now like to add websocket code to the server.
Most examples I see have some setup code similar to:
const http = require('http');
const server = http.createServer();
const wsServer = new WebsocketServer({httpServer: server});
When I run 'npm run serve' I now am greeted with the following error message:
'http.createServer is not a function'
Is there a way to attach the websockets to the current running node server when invoked via npm run serve? In other words, can I skip the createServer call and attach it to whatever is currently running?
Well, your code should read something like this...
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
You will need to use ExpressJS on the back-end to handle messages.

Using socket.io from a module

My sio = require('socket.io').listen(app) is in my server.js file, but I'm calling a method in a library that would like to push a message to the client... say api.user.pushToClient()
How am I able to access sio.sockets from there? Perhaps my structure is incorrect?
Folder structure:
server.js
api
|--user.js
|--another.js
in server.js append this line
module.exports.sio = sio;
in api/user.js
sio = require('../server').sio;
sio.sockets.on ...
Or did I misunderstand the question?
What I understood from the question is you want to know how to use socketIO with node module.Based on my understanding you can use it as below:
First install socketIO module locally with npm by running " $npm install socket.io " command for windows.
Add Script to your HTML page:
<script src="/socket.io/socket.io.js"></script>
Now add var io = require('socket.io'); to your server or js file where you are going to use it.
Then you can have server startup code listen to that server and on connection of it perform the options for any event.
var listener = io.listen(server);
listener.sockets.on('connection', function(socket) {
socket.on('locationClick', function(data) {
// perform the function on receving locationClick event.
}
}

nodejs, expressjs serving PHP files

Ok I've been playing around with nodejs, expressjs and socket.io to create some applications. But now im coming to the stage where i want to take things a bit further.
I have noticed a few node apps using PHP for twitter auth on their client side. When I attempt to rename my client.html file to client.php and restart the server it throws up a blank page with this
Cannot GET /
How do would serve php files or would i do twitter auto using js?
This is my NodeJS server.js
var http = require('http'),
express = require('express');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
app.listen(1234);
console.log("server started on port :1234");
As already noted, node.js itself won't interpret php files for you. Available options are:
have nginx in front of node and reverse proxy all request you want to process in node from it
proxy php requests from node to php-capable server
spawn php process on each request in node
make fastcgi link to php-fastcgi using node fastcgi protocol parser
Uh, skip PHP entirely and integrate everyauth into your app.
PHP-EXPRESS should do. Just install the package and follow the docs.
https://www.npmjs.com/package/php-express

Resources