{"library":"socket.io-cookie-parser","title":"Socket.IO Cookie Parser Middleware","type":"library","description":"socket.io-cookie-parser is a middleware specifically designed for Socket.IO applications, enabling the parsing of HTTP cookies on incoming WebSocket connections. Currently at stable version 1.0.0, this package acts as a thin wrapper around the widely used `express-cookie-parser` library, allowing developers to seamlessly share cookie-based session and authentication data between their Express.js HTTP server and Socket.IO real-time layer. Its primary utility lies in simplifying the process of accessing client-side cookies, making them available on `socket.request.cookies` and `socket.request.signedCookies`. This facilitates consistent authentication and state management in applications utilizing both traditional HTTP routes and WebSockets. The package is typically stable, following the lifecycle of its `express-cookie-parser` dependency, with new releases primarily addressing compatibility or minor enhancements.","language":"javascript","status":"maintenance","last_verified":"Thu Apr 23","install":{"commands":["npm install socket.io-cookie-parser"],"cli":null},"imports":["const cookieParser = require('socket.io-cookie-parser');","io.use(cookieParser('secret', { /* options */ }));","socket.request.cookies;"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/mhuggins/socket.io-cookie-parser","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/socket.io-cookie-parser","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"const express = require('express');\nconst http = require('http');\nconst socketio = require('socket.io');\nconst cookieParser = require('socket.io-cookie-parser');\n\nconst app = express();\nconst server = http.createServer(app);\nconst io = socketio(server);\n\n// Use the cookie parser middleware\n// 'keyboard cat' is a secret for signing cookies. Use a strong secret in production.\nio.use(cookieParser('keyboard cat', {\n  decode: function (str) {\n    // Example custom decoding function, optional.\n    // Defaults to decodeURIComponent.\n    return str.replace(/%20/g, ' '); // Simple example, usually not needed.\n  }\n}));\n\n// Example authorization middleware using parsed cookies\nio.use((socket, next) => {\n  const cookies = socket.request.cookies;\n  const signedCookies = socket.request.signedCookies;\n\n  console.log('Incoming connection. Raw headers:', socket.request.headers.cookie);\n  console.log('Parsed cookies:', cookies);\n  console.log('Parsed signed cookies:', signedCookies);\n\n  // A simple authorization check based on a signed cookie\n  if (signedCookies && signedCookies.auth_token === 'super_secret_token') {\n    console.log('Client authorized:', socket.id);\n    next(); // Authorize the connection\n  } else {\n    console.log('Client unauthorized:', socket.id);\n    next(new Error('Authentication required.')); // Reject the connection\n  }\n});\n\nio.on('connection', (socket) => {\n  console.log(`User connected: ${socket.id}`);\n\n  socket.on('disconnect', () => {\n    console.log(`User disconnected: ${socket.id}`);\n  });\n\n  socket.emit('status', 'Welcome! Your session is active.');\n});\n\napp.get('/', (req, res) => {\n  res.send('<h1>Socket.IO with Cookie Parser</h1><p>Connect with a client to see cookie parsing in action.</p>');\n});\n\nserver.listen(3000, () => {\n  console.log('Server listening on http://localhost:3000');\n  console.log('Socket.IO listening for connections.');\n});","lang":"javascript","description":"This quickstart demonstrates how to integrate `socket.io-cookie-parser` into a Socket.IO server, showing how to parse both regular and signed cookies, and then utilize them within a Socket.IO authorization middleware.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}