score:1
right, what i should do is just use cookies for your session. that way any front end ajax calls (including websocket) will use the cookie in the request headers.
with the http only
setting set on the cookie, no front end javascript can modify (or access) the session cookie, but it is still used by the browser on any outgoing request.
in my opinion this is the safest way for using the session id, without the front end needing to know about the existence of the cookie in the first place.
if the session in koa doesn't exist any more, you automatically know the user is logged out as well.
i've made a little example for this (there is a github link below):
first the index.js
:
'use strict';
const session = require('koa-session');
const koa = require('koa');
const websockify = require('koa-websocket');
const route = require('koa-route');
const app = websockify(koa());
app.keys = ['some secret hurr'];
const sessionstore = session(app);
app.use(sessionstore);
app.ws.use(sessionstore);
app.use(route.all('/', function* (next) {
// ignore favicon
if (this.path === '/favicon.ico') return;
let n = this.session.views || 0;
this.session.views = ++n;
yield next;
}));
app.ws.use(route.all('/', function* (next) {
this.websocket.on('message', (message) => {
let n = this.session.views || 0;
this.session.views = ++n;
if (message === 'ping') {
// return the amount of sessions (n) when the client sends ping
this.websocket.send('pong ' + n);
}
});
yield next;
}));
app.use(require('koa-static')('./public'));
app.listen(3000);
console.log('listening on port 3000');
and then the index.html
:
<html>
<script>
var ws = new websocket("ws://localhost:3000/");
ws.onopen = function() {
// sends a message
ws.send("ping");
};
ws.onmessage = function(e) {
// receives a message.
alert(e.data);
};
ws.onclose = function() {
alert("closed");
};
</script>
</html>
i've put all of this in to a working example on github.
Source: stackoverflow.com
Related Query
- NodeJS: What's the best way to share session between server-rendered views and React app
- What's the best way to deal with an error in the server side and in the client side using nodejs + express
- what is the best way to reuse code between NodeJS and React/Webpack?
- What would be the best way to differentiate between regular users and administrators in firebase?
- what's the best way to share user data between components?
- Whats the best way to update an object in an array in ReactJS?
- What is the best way to manage a user's session in React?
- React - What is the best way to handle login and authentication?
- Whats the best way to conditionally add readOnly in React?
- Whats the difference between type babel and jsx
- Whats the difference between ForwardRefExoticComponent and ForwardRefRenderFunction in react?
- how to share environment variables between react app and express js server hosting it as static site
- What is the simplest way to share react components between projects?
- Whats is the difference between index.js and _app.js in NextJs?
- What is the difference between server side rendering (Next.js) and Static Site rendering (Gatsby.js)?
- Best solution to share code between 2 separate projects React Js and React Native? ( like redux store)
- What is the best and most efficient way to bind callbacks in ReactJS? In the constructor or in the render method or as a property initializer?
- Error: Hydration failed because the initial UI does not match what was rendered on the server with useSession() and react-bootstrap
- If a function updates a state variable which is an object and is called from a child component, what's the best way to avoid infinite renders?
- Sharing session between React front-end server and PHP/Symfony back-end server
- Share GraphQL schema between client and server
- javascript Share code between my client and my server folders
- Best way to navigate between pages and handle back button in react js
- What is the best way to save my dynamicly created data using React and Javascript?
- what is the best way to use react router V6 navigation with redux and redux thunk actions?
- What is the best way to apply layout styling to styled-components and React components used in different places on a site?
- What is the correct way of setting state variables in Reactjs and what is the difference between these approaches?
- What is the best way for interacting between Components in Reactjs?
- React: how can you share the state between views using react-router?
- In graphql, what is the best way to combine multiple sources where fields in both sources share a common value?
More Query from same tag
- React map inside map
- How to change input value which is coming from props in React?
- React: TodoList: Give an Input to an Array
- Cant fetch data from child component react
- React JS doesn't render on first time
- React Redux how to dispatch async actions and update state
- How to pass props and function to the react-widow list?
- Cannot find module 'js' using express
- Not able to change color of NavLink (React Bootstrap)
- What does setDoc function return?
- React: How to prevent a setInterval from being delayed after a button is clicked to start it?
- React state gets updated only after I submit the form twice
- Django back end with a React front end
- why can't I render component from inside .map in reactjs?
- React: Keyboard Event Handlers All 'Null'
- "WebExtension::executeScript: content script injected VM149:1" in Chrome loading React app
- Update Button state onClick React
- React - console logs empty an array and then logs a populated array after a second button click
- How to trigger images to load in background using Next.js
- Changing state based off result of helper function in component
- Why 1 value from API doesn't not save?
- How to pass a custom style to MUI V5 styled component?
- Why can I not get a reference to my component from the ReactDOM.render() callback?
- Detecting user leaving page with react-router
- React - 2 input field dependent on each other - infinite loop
- Why redux toolkit boolean toggle doesn't work?
- React Js with Visual studio 2017 with Typescript
- Jest testing - how to handle JsonWebToken response
- React Hooks Calling GET Request On Modal Popup
- I received Object(...) is not a function when I use useGetCryptoNewsQuery from api and try to console.log(cryptoNews)