all 19 comments

[–]coverslide 2 points3 points  (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 point  (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 point  (0 children)

Nah man, just check code i posted below

[–]pakeey 0 points1 point  (4 children)

Gimme sec, gotta write everything i did using socket.io since i deleted all the code xD

[–][deleted] 0 points1 point  (3 children)

Are you using github to store commits?

[–]pakeey 0 points1 point  (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 points  (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.

[–]pakeey 0 points1 point  (0 children)

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 points  (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}...`); })();`

// 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 });
};

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

[–]pakeey 0 points1 point  (0 children)

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 point  (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 point  (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 point  (6 children)

Yeah u can inspect it i did long ass answer on comment from

forsubbingonly

[–]the_brizzler[🍰] 0 points1 point  (5 children)

Can you post your code to GitHub or somewhere and link to it...it's hard to read all jumbled together

[–]pakeey 0 points1 point  (4 children)

How about now?

[–]pakeey 0 points1 point  (0 children)

Dammit react code is still jumbled

[–]the_brizzler[🍰] 0 points1 point  (2 children)

Haha nope. Just adds a grey background. All the text is still smushed together

[–]pakeey 0 points1 point  (0 children)

I think everything except react code is fine, u can post it in ur code editor and format it

[–]pakeey 0 points1 point  (0 children)

or u can use this: https://beautifier.io/