I've set up an API with nodejs express for a real-time chat application. For it to be real-time I am using primus but I'm currently stuck at trying to connect primus to my frontend.
I have a folder structure for the whole backend and then another folder structure for my frontend. So they are both separate.
Here I connect the server to Primus
var server = http.createServer(app);
const primus = require('../primus/live').go(server);
This then goes as you can see to the folder primus with a file live.js
//BACKEND
const Primus = require('primus');
let go = (server) => {
let primus = new Primus(server, {/* options */});
primus.on('connection', (spark) => {
console.log('Received spark 🔥');
});
}
module.exports.go = go;
Now in my frontend, I am trying to call Primus via the script tag
//FRONTEND
<script src="http://localhost:3000/primus/live.js"></script>
but this just gives me a 404 Not Found error. Also when I just try to connect through this in my browser it doesn't work. So I am unsure what my problem here is. Any ideas?
https://github.com/primus/primus#how-do-i-use-primus-with-express
make sure to call .listen on the http server, not the Express server
Related
I have a Frontend application sending file over HTTP to Nodejs Backend. The Nodejs application acts as a socketio server and needs to send the file to a python socketio client. I have used multer to parse the HTTP request. Here are the nodejs and python code I have written so far (only pasted required parts)
// NodeJS server code I have written so far (could be completely wrong)
// req.file contains the file parsed from multer
var socket = req.app.get('socket');
let readStream = createReadStream(req.file.path);
readStream.on('ready', () => {
socket.emit('image', readStream, data => console.log(data));
readStream.pipe(createWriteStream(req.file.originalname));
});
# Python socketio client
#sio.event
async def image(data):
try:
print(data)
# want to save data preferably in a local file or a buffer is also fine
except:
return 'NOT OK'
finally:
return 'OK'
I am facing issues on both fronts, for both of which I have almost no idea how to procede:
sending image from nodejs (thought of using fs.createReadStream but don't know how to use the stream over a socketio event)
receiving image on python and storing it locally.
I am just learning webdev and want to try to make a multiplayer game using Express and socket.io
I can make a server with socket.io in it which listens. That part works fine.
However when I try to connect a client, this only works if I let the HTML file with the following in it be served by the server like follows:
Server code:
const express = require('express')
const app = express()
const http = require('http')
const server = http.createServer(app)
const { Server } = require('socket.io')
const io = new Server(server)
const port = 3000
io.on('connection', (sock) => {
console.log('client connected')
})
// This seems to be necessary, but I don't want it to be!!!
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
server.listen(port, () => {
console.log(`Server listening on port ${port}`)
})
This index.html has the following at the bottom:
<script src="/socket.io/socket.io.js"></script>
<script>const socket = io()</script>
However I want to keep all my frontend code seperate from the server. I made a seperate repository for the frontend and backend. I want the frontend to contain all the UI logic and use only data calls (AJAX) to get Json data from the server. So I want to put this index.html file in my frontend code only.
Yet if I do this the connection doesn't work.
I can start the server fine.
I open index.html from WebStorm which also creates a server for this which I configured to also listen to port 3000
Yet it cannot find /socket.io/socket.io.js and I get the following error in the console.
It also doesn't work if WebStorm runs on a different port.
The resource from “http://localhost:3000/socket.io/socket.io.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
How can I keep this html in my client repo only and still work with socket.io, or is this not possible?
You can't have multiple servers listening on the same port. Run the servers on different ports and either:
Have a reverse proxy forwarding requests to your Socket.io server (which needs special handling) and your front end server or
Put an absolute URL in the script src and configure CORS.
How can i run this code from angular2 app component?
var request = require("request");
var fs = require("fs");
var img = "http://vignette1.wikia.nocookie.net/custombionicle/images/a/ae/Kitten_with_gun_final.png";
request(img).pipe(fs.createWriteStream('image.png'));
Unfortunately, node.js is the backend part of the website and angular the frontend, you can't mix them together. What you can do instead is to run the pice of code on the backend and if there's some data you need it on the client side, to send it to the client ;)
I am pretty new to node.js and now I am doing a project on building a website on node.js. Sorry if my question is very naive.
I am using express framework.
My app.js is listening at port (3000).
In my route.js, I got some data from calling some API. I want to display the data to my datapoint.jade file. And because I want to draw those datapoint I have to embed a javascript file in jade. I want to use socket.io to achieve this sending and receiving data.
However, all the examples to construct socket is in app.js. I get data in router.js(in one of its callback function). app.js require router.js.
How can I send data from route.js to app.js and then send out to client side.
Could you please guide me some related and useful information? Or my design would not work at all?
A common paradigm is to pass any dependencies to your child modules:
// app.js
var socket = require('socket.io');
var app = require('express')();
var routes = require('./routes.js')(app, socket);
app.listen();
// routes.js
module.exports = function(app, socket){
var routes = {};
app.use('/', routes.handleIndex)
socket.on('connection', function(){
...
})
}
I'm working on a project which consists in creating a game of the goose like. In order to do that, I'm using Node.js, Express, jade and now Socket.io. But I encounter some trouble, like, in example, to share the position of one client to the other client. Because my variable position is in a function in index.js and I don't know how I can use Socket.io in a route file. I try some things, but nothing works.
On internet, I've seen some people who say that there is no-sense to use Socket.io in an express route file. So how can I do that ?
In my index.js I've that :
exports.deplacement = function(io)
{
return function(req,res)
{
//[...]
io.sockets.on('connection', function(socket)
{
socket.broadcast.emit('position', space);
});
res.render('moteur' //[...]);
}
}
And in my moteur.jade I've done this :
script(src="/socket.io/socket.io.js")
script.
var socket = io.connect('http://localhost:3000');
socket.on('position ', function(space) {
alert(space);
})
First of all, I'm not sure what your question exactly means, but if it is what I think it is then I think what you mean by using socket.io in a route file is to be able to include the client side javascript lib provided with socket.io module of Node.
In order to do that, you have to allow the socket.io module to listen to server. This works like a middle-ware itself. Everything has to go through socket.io first before they are routed to the server. So, when you request the client side lib, it is uploaded to the client.
var express = require('express')
, routes = require('./routes')
, http = require('http');
var app = express();
var server = app.listen(3000);
var io = require('socket.io').listen(server)