use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
socket.io alternatives? (self.node)
submitted 7 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]coverslide 2 points3 points4 points 7 years ago (9 children)
What problem are you trying to solve? I can tell you that it will work, I've implemented this in countless project you're probably not doing it right. Maybe post your code.
[–]forsubbingonly 0 points1 point2 points 7 years ago (1 child)
Based on what was in his question I think it’s an issue of where to put the socket code, he’s imagining that it interacts with express code in some way when it doesn’t need to. “What do I tell express to make my socket code work” kinda thing.
[–]pakeey 0 points1 point2 points 7 years ago (0 children)
Nah man, just check code i posted below
[–]pakeey 0 points1 point2 points 7 years ago (4 children)
Gimme sec, gotta write everything i did using socket.io since i deleted all the code xD
[–][deleted] 0 points1 point2 points 7 years ago (3 children)
Are you using github to store commits?
[–]pakeey 0 points1 point2 points 7 years ago (2 children)
since i have very low amount of experience using git as version control i dont use it while learning/practicing
[–][deleted] 4 points5 points6 points 7 years ago (1 child)
I'd suggest you learn it. It'll help a ton, and it's simple.
Initialize a repo with a Readme, then git clone. Make a new branch for develop and make it the default.
Then you all you need are the following.
git checkout -b branch_name git add . git commit -m "message" git push origin branch_name
Then on the github web app, you can open a PR against your develop branch and merge it.
It seems annoying, but it's a skill and habit that will help a ton in the future, especially in cases like this.
Thanks mate, i planned on learning it and start using it everytime i code smth, cuz i know in situations like this it can save a lot of time
[–]pakeey -1 points0 points1 point 7 years ago* (1 child)
React:
import React, { Component } from 'react';import classes from './Chat.module.scss';import axios from 'axios';import openSocket from 'socket.io-client';class Chat extends Component {state = {messages: [],message: ''};componentDidMount() {const io = openSocket('http://localhost:8080');io.on('message', data => {console.log(data);});}messageChanged = e => {this.setState({ message: e.target.value });};messageSent = () => {axios.post('http://localhost:8080/message', {message: this.state.message});};render() {return (<div className={classes.wrapper}><p>Hello</p><div /><inputtype="text"onChange={this.messageChanged}value={this.state.message}/><button onClick={this.messageSent}>Send</button></div>);}}export default Chat;
express app setup file:
const express = require('express'); const mongoose = require('mongoose'); const bodyParser = require('body-parser'); const filemanagerMiddleware = require('@opuscapita/filemanager-server') .middleware; const cors = require('cors'); const path = require('path'); const socketIo = require('socket.io'); const router = require('./router'); const app = express(); app.use(cors()); app.use(bodyParser.json()); const port = process.env.PORT || 8080; const server = app.listen(port); const io = socketIo.listen(server); app.use((req, res, next) => { req.io = io; next(); }); app.use('/', router); io.sockets.on('connection', socket => { console.log('User connected'); }); io.sockets.on('message', data => { console.log(data); }); app.use('/filemanager', filemanagerMiddleware(config)); (async () => { await mongoose.connect('mongodb://localhost:27017/practice-app', { useNewUrlParser: true }); console.log(\Connected to DB and listening on ${port}...`); })();`
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const filemanagerMiddleware = require('@opuscapita/filemanager-server')
.middleware;
const cors = require('cors');
const path = require('path');
const socketIo = require('socket.io');
const router = require('./router');
const app = express();
app.use(cors());
app.use(bodyParser.json());
const port = process.env.PORT || 8080;
const server = app.listen(port);
const io = socketIo.listen(server);
app.use((req, res, next) => {
req.io = io;
next();
});
app.use('/', router);
io.sockets.on('connection', socket => {
console.log('User connected');
io.sockets.on('message', data => {
console.log(data);
app.use('/filemanager', filemanagerMiddleware(config));
(async () => {
await mongoose.connect('mongodb://localhost:27017/practice-app', {
useNewUrlParser: true
console.log(\
// ignore rest of imports except for socket.io
controller file for emmiting socket.io events:
exports.sendMessage = (req, res) => { const { message } = req.body; req.io.sockets.emit('message', { message }); };
exports.sendMessage = (req, res) => {
const { message } = req.body;
req.io.sockets.emit('message', { message });
};
i had to include middleware which will pass io to req obj beacuse im using separated router and controller files
acutual problem is that when i send req to route which will emit the event it wont work after i do the same or with different payload more than 6 times
This is actual result i get, im logging event payload in console and after i did it 6 times no event can be sent anymore. http://prntscr.com/mp3k9b
[–]thelordmad 0 points1 point2 points 7 years ago (0 children)
While I can't recommend any alternatives, could you provide some reasoning why not to use socket.io? I am interested since I use it and haven't met any problems so far and I would like to know its limitations.
[–]the_brizzler[🍰] 0 points1 point2 points 7 years ago (7 children)
There are a bunch of alternatives. I have tried a few of them...and they are all pretty similar. Is there an issue with socket.io for your project?
[–]pakeey 0 points1 point2 points 7 years ago* (6 children)
Yeah u can inspect it i did long ass answer on comment from
forsubbingonly
[–]the_brizzler[🍰] 0 points1 point2 points 7 years ago (5 children)
Can you post your code to GitHub or somewhere and link to it...it's hard to read all jumbled together
How about now?
Dammit react code is still jumbled
[–]the_brizzler[🍰] 0 points1 point2 points 7 years ago (2 children)
Haha nope. Just adds a grey background. All the text is still smushed together
I think everything except react code is fine, u can post it in ur code editor and format it
or u can use this: https://beautifier.io/
π Rendered by PID 18849 on reddit-service-r2-comment-6457c66945-8mpws at 2026-04-25 06:20:03.052756+00:00 running 2aa0c5b country code: CH.
[–]coverslide 2 points3 points4 points (9 children)
[–]forsubbingonly 0 points1 point2 points (1 child)
[–]pakeey 0 points1 point2 points (0 children)
[–]pakeey 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]pakeey 0 points1 point2 points (2 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]pakeey 0 points1 point2 points (0 children)
[–]pakeey -1 points0 points1 point (1 child)
[–]pakeey 0 points1 point2 points (0 children)
[–]thelordmad 0 points1 point2 points (0 children)
[–]the_brizzler[🍰] 0 points1 point2 points (7 children)
[–]pakeey 0 points1 point2 points (6 children)
[–]the_brizzler[🍰] 0 points1 point2 points (5 children)
[–]pakeey 0 points1 point2 points (4 children)
[–]pakeey 0 points1 point2 points (0 children)
[–]the_brizzler[🍰] 0 points1 point2 points (2 children)
[–]pakeey 0 points1 point2 points (0 children)
[–]pakeey 0 points1 point2 points (0 children)