Using passport for socket.io - javascript

Hi im trying to use passport for authencication, But im not sure what to do on the client side. I think the server side is fine, but it gives me this error:failed: WebSocket is closed before the connection is established. So it seems like it cant get the connection. what do i need to send from the client side, and do i need to change something on server side??
please heeeelp thanks:)
server.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require("socket.io")(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
var port = process.env.PORT || 4000;
const passportSocketIo = require('passport.socketio')
const cookieParser = require('cookie-parser')
const session = require("express-session")
const passport = require('passport')
io.use(passportSocketIo.authorize({
cookieParser: cookieParser,
key: 'express.sid',
secret: 'session_secret',
store: session,
success: onAuthorizeSuccess,
fail: onAuthorizeFail,
}));
function onAuthorizeSuccess(data, accept){
console.log('successful connection to socket.io');
// The accept-callback still allows us to decide whether to
// accept the connection or not.
accept(null, true);
// OR
// If you use socket.io#1.X the callback looks different
accept();
}
function onAuthorizeFail(data, message, error, accept){
if(error)
throw new Error(message);
console.log('failed connection to socket.io:', message);
// We use this callback to log all of our failed connections.
accept(null, false);
// OR
// If you use socket.io#1.X the callback looks different
// If you don't want to accept the connection
if(error)
accept(new Error(message));
// this error will be sent to the user as a special error-package
// see: http://socket.io/docs/client-api/#socket > error-object
}
io.on('connection', function (socket) {
console.log("connection")
var addedUser = false;
// when the client emits 'new message', this listens and executes
socket.on('new message', function (data) {
var data = validator.escape(data);
// we tell the client to execute 'new message'
socket.broadcast.emit('new message', {
username: socket.username,
message: data
});
db.serialize(function() {
// console.log('inserting message to database');
var insertMessageStr = "INSERT INTO messages (username, content, posted) VALUES ('" + socket.username + "','" + data.toString() + "'," + Date.now() + ");"
// console.log(insertMessageStr)
db.run(insertMessageStr);
});
});
client.js
const socket = io({
key: 'express.sid',
secret: 'session_secret',
});

Related

Create rest api to retrieve data from odoo erp

I am creating a rest api service to get, put, delete data from odoo erp .
Here is my code :
const Promise = require('bluebird');
Promise.promisifyAll(require('node-odoo').prototype);
const Odoo = require('odoo-xmlrpc');
const odoo = new Odoo({
url: 'zzzz',
port: 'zz',
db: 'zzzz',
username: 'zzzz',
password: 'zzz*'
});
var express = require('express'),
app = express(),
port = process.env.PORT || 3000;
this.router = express.Router();
app.listen(port);
console.log('todo list RESTful API server started on: ' + port);
this.router.get('/api/event/', (req, res) => {
return getEvent(req, res);
});
app.get('/getEvent', (request, response) => {
odoo.connect((err) => {
if(err) return console.log('Findeventlist error ' + err);
console.log('Findeventlist connected ' );
var inParams = [];
inParams.push([]);
inParams.push(['name' ])
inParams.push(0)
inParams.push(5)
var params = [];
params.push(inParams);
odoo.execute_kw('calendar.event', 'search_read', params, function (err, value) {
if (err) { return console.log(err) }
if(value){
console.log( 'Value is ' + response.status(200).json(value));
return response.status(200).json(value)
}
});
});
console.log(' odoo connected');
})
I got this error : Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
Can you help me please where i'm wrong?
response.status(200).json(value) responds to the client and ends the transaction (just like response.end() or response.sendFile() etc).
It must be called only once, but you execute it twice (once inside the console.log() then once "for real" the next line). That's why the headers are "already sent".
Remove the useless response.json() from the console.log and log only the value you want to see.

How to emit a message to the sockets from an API endpoint?

I have socket.io set up and working and now I need to send updates to the users via the same sockets, the updates I get from a different server (the other server makes a GET http request to my nodejs server and I need to take the data from that http request and emit it to a certain user via sockets)
Here's my code, emitting sockets from inside the socket process works fine but from inside the API call doen't work.
var express = require("express");
var app = express();
var http = require("http").createServer(app);
var io = require("socket.io")(http);
app.set('socketIo', io);
io.on("connection", (socket) => {
console.log("User connected: ", socket.id);
io.to(socket.id).emit("new_message", {id: "999", msg: "You are connected, your Id is " + socket.id});
})
app.get("/send/:id/:message", (request, result) => {
const ioEmitter = request.app.get("socketIo");
ioEmitter.to(request.params.id).emit({ id: "999", msg: request.params.message });
result.send("Sending message to socket Id: " + request.params.id)
console.log("Sending message to socket Id: " + request.params.id);
})
const port = 3001;
http.listen(port, () => {
console.log("Listening to port " + port);
});

How can I emit to a single client with socket.io (rather than everyone)

Combing through the documentation but no luck. I'm trying to emit to a single client/user rather than everyone.
I read several other questions about this, most have no answer or point to older solutions. Any help would be appreciated.
The following works but emits to everyone on the site rather than the individual user...
SERVER:
//Socket.io
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.post('/login', (req, res) => {
const email = cryptonize.encrypt(req.body.email);
const password = cryptonize.encrypt(req.body.password);
io.emit('login success', email, password);
});
CLIENT:
const socket = io();
socket.on('login success', (user, token, auth) => {
console.log(`user:${user}, password:${password});
});
I've tried "socket.emit" as mentioned in the socket.io cheat sheet, but it's coming back as undefined on the server. Probably something really simple I'm missing, just not seeing it.
I don't think that is the intended use of socket.io.
In your case, a simple res.end(...) will do (at least based on what you showed us).
app.post('/login', (req, res) => {
const email = cryptonize.encrypt(req.body.email);
const password = cryptonize.encrypt(req.body.password);
res.end(/* data */);
});
Read the docs about res.end().
If you really need to emit to a single socket, you need more work:
Use socket.io's rooms or namespace.
Get target socket's id.
Emit using the socket id.
Here's an example using default namespace:
Server
const IO = require('socket.io');
const io = IO(server);
// Default namespace is '/'
const namespacedIO = io.of('/');
io.on('connection', socket => {
socket.on('send', data => {
const targetSocket = namespacedIO.connected[data.socketID];
targetSocket.emit('received', data.value);
});
});
Client
const socket = io();
submit.addEventListener('click', function(){
socket.emit('send', {
socketID: socket.id, // IMPORTANT: to get the source socket ID
value: input.value
});
})
socket.on('received', function(data){
console.log(`Data "${data}" is received at server.'`);
});
Here's what I ended up doing, for anyone else trying to figure this out.
SERVER:
//Socket.io
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.post('/login', (req, res) => {
const email = cryptonize.encrypt(req.body.email);
const password = cryptonize.encrypt(req.body.password);
const socketid = req.query.socket;
io.sockets.connected[socketid].emit('login success', email, password);
});
CLIENT:
const socket = io();
let socketid;
socket.on('connect', () => socketid = socket.io.engine.id);
CLIENT cont..
Then I just added a "socketid" query to my posts as they're generated.
//XHR Setup
const xhr = new XMLHttpRequest();
let response, status, readyState;
xhr.onreadystatechange = () => {
if (xhr.status === 200 && xhr.readyState === 4) response = xhr.response;
};
//XHR POST
const post = ({ url, callback, data }) => {
xhr.open('POST', `${url}&socketid=${socketid}`, true), xhr.setRequestHeader('Content-type', 'application/json'), xhr.send(data);
if (callback) callback();
console.log(`${url}&socketid=${socketid}`);
}

socketio-jwt: Connected to SocketIO, Authenticating

I've followed multiple tutorials to set up socketio-jwt, but every time it seems that I'm not getting past this part:
Connected to SocketIO, Authenticating
Any ideas?
Client side:
<h1>Socket Connection Status: <span id="connection"></span></h1>
<script type="text/javascript">
$(document).ready(function () {
socketIOConnectionUpdate('Requesting JWT Token from Laravel');
$.ajax({
url: 'http://localhost:8000/token?id=1'
})
.fail(function (jqXHR, textStatus, errorThrown) {
socketIOConnectionUpdate('Something is wrong on ajax: ' + textStatus);
})
.done(function (result, textStatus, jqXHR) {
socketIOConnectionUpdate('Connected to SocketIO, Authenticating')
/*
make connection with localhost 3000
*/
var token = result.token;
var socket = io.connect('http://localhost:3000');
socket.on('connect', function () {
socket
.emit('authenticate', {token: token}) //send the jwt
.on('authenticated', function () {
console.log('authenticated');
socketIOConnectionUpdate('Authenticated');
})
.on('unauthorized', function(msg) {
socketIOConnectionUpdate('Unauthorized, error msg: ' + msg.message);
throw new Error(msg.data.type);
})
});
});
});
/*
Function for print connection message
*/
function socketIOConnectionUpdate(str) {
$('#connection').html(str);
}
Server side
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var socketioJwt = require('socketio-jwt');
var dotenv = require('dotenv').config({path:'../.env'});
var port = 3000;
io
.on('connection', socketioJwt.authorize({
secret: dotenv.JWT_SECRET,
timeout: 100 // 15 seconds to send the authentication message
}))
.on('authenticated', function(socket){
console.log('connected & authenticated: ' + JSON.stringify(socket.decoded_token));
socket.on('chat message', function(msg){
debugger;
io.emit('chat message', msg);
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
You may have misunderstood how dotenv works, as you're trying to use it's return value.
Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env.
From: dotenv github
Instead, it exports the variables stored in the file located at ../.env as environment variables, that become available as a part of process.env.
So instead of this:
var dotenv = require('dotenv').config({path:'../.env'});
socketioJwt.authorize({
secret: dotenv.JWT_SECRET,
timeout: 100
})
Do this
// do this near the entry point to your application!!
require('dotenv').config({path:'../.env'});
socketioJwt.authorize({
secret: process.env.JWT_SECRET,
timeout: 100
})

How to emit an event in socket.io from the routes file?

This is my app configuration
app.js
//SERVER
var server = app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", app.get('port'),
app.settings.env);
});
//SOCKET.IO
var io = require('./socket.io').listen(server)
/socketio
var socketio = require('socket.io')
module.exports.listen = function(app)
{
io = socketio.listen(app);
io.configure('development',function()
{
//io.set('transports', ['websocket', 'xhr-polling']);
//io.enable('log');
});
io.configure('production',function()
{
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.set('log level', 1); // reduce logging
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
});
io.sockets.on('connection', function (socket)
{
console.log("new connection: "+socket.id);
socket.on('disconnect',function(){console.log("device "+socket.id+" disconnected");});
socket.emit('news', { hello: 'world' });
socket.on('reloadAccounts',function(data)
{
var accounts=['account1','account2']
socket.emit('news',accounts)
});
});
return io
}
/routes
exports.newAccount=function(fields,callback)//localhost:3000/newAccountForm
{
//... bla bla bla config db connection bla bla bla
db.collection('accounts').insert(fields,function(err,result)
{
if(err)
{
console.warn(err);
db.close();
return callback(err,null);
}else{
if(result)
{
db.close();
return callback(null,result);
socket.emit('new account created',result) // i want to emit a new event when any user create an account
}else{
db.close();
return callback('no se consigue resultado',null);
}
}
})
});
}
How to emit an event in socket.io from the routes file?
First you need to decide that what socket you want to send the new info. If it's all of them(to everyone connected to your app), it would be easy, just use io.sockets.emit:
In the ./socket.io file you add exports.sockets = io.sockets; somewhere after io = socketio.listen(app);. Then in the routes file, you can emit like this:
var socketio = require('./socket.io');
socketio.sockets.emit('new account created', result);
If you know the socket id that you want to send to, then you can do this:
var socketio = require('./socket.io');
socketio.sockets.sockets[socketId].emit('new account created', result);
You can also select the socket by express session id:
First you need to attach the session id to the socket on authorization:
io.set('authorization', function (data, accept) {
// check if there's a cookie header
if (data.headers.cookie) {
// if there is, parse the cookie
data.cookie = cookie.parse(data.headers.cookie);
// note that you will need to use the same key to grad the
// session id, as you specified in the Express setup.
data.sessionID = data.cookie['express.sid'];
} else {
// if there isn't, turn down the connection with a message
// and leave the function.
return accept('No cookie transmitted.', false);
}
// accept the incoming connection
accept(null, true);
});
Then you can select sockets with the session id:
var socketio = require('./socket.io');
var sockets = socketio.sockets.forEach(function (socket) {
if (socket.handshake.sessionID === req.sesssionID)
socket.emit('new account created', result);
});
You can also query your session store and using the method I described above, emit the event to sockets with sessionId that matched your query.

Categories