W
W
WebLedNik2021-02-02 13:55:37
Socket.io
WebLedNik, 2021-02-02 13:55:37

How to connect a socket for a specific page of a site?

Good afternoon.

I am building an app using Vue + Express. Decided to use socket.io in the project.
Vue runs the client side on port 8080, and I run the server on port 3000.
For the client, I use the socket.io-client library. I use socket.io for the server.

The crux of the problem:
I want it to be displayed only when I go to the page localhost:8080/books . It is actually displayed when I go to localhost:8080 . app.js code:console.log('Connected')console.log('Connected')

const express = require('express')
const history = require('connect-history-api-fallback');
const app = express()
app.use(history());

const server = require('http').createServer(app)
const options = {
  cors: {
    origin: "http://localhost:8080",
    methods: ["GET", "POST"]
  }
}

const io = require('socket.io')(server, options)
const config = require('config')
const path = require('path')
const cors = require('cors')
const { v4 } = require('uuid')

const PORT = config.get('port')

app.use(cors())

// app.use('/', express.static(path.join(__dirname, '/client/dist')));

const books = io.of('/books')
books.on('connection', (socket) => {
  console.log('Connected')
  socket.on('disconnect', () => {
    console.log('Disconnected')
  })
})

server.listen(PORT, () => console.log(`Server start on ${PORT}`))

Code in App.vue:
import { io } from "socket.io-client";

const socket = io.connect("http://localhost:3000/books");

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question