O
O
Oleg2019-04-09 16:55:34
React
Oleg, 2019-04-09 16:55:34

How to configure Webpack to work with WebSocket?

I have a React project. We want to implement WebSocket there so that changes from the backend are pulled up without reloading the page.
My task is to set up webpack so that it connects, for example, here https://www.websocket.org/echo.html. Actually the question is how to do it?
In the webpack config, the piece with devServer looks like this:

devServer: {
        contentBase: path.resolve(__dirname, `./${folder}`),
        compress: true,
        port: 9000,
        host: '0.0.0.0',
        open: false,
        historyApiFallback: true,
        proxy: [{
            context: ['/api', '/dash'],
            target: 'url_нашего_сервера',
            changeOrigin: true,
            secure: true
        }]
    },

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2019-04-10
@r45her

Sample solution:

devServer: {
    contentBase: path.resolve(__dirname, `./${folder}`),
    compress: true,
    port: 9000,
    host: '0.0.0.0',
    open: false,
    historyApiFallback: true,
    proxy: [
        {
            context: ['/api', '/dash'],
            target: 'https://${systemUrl}/',
            changeOrigin: true,
            secure: true
        },
        {
            context: ['/websocket'],
            target: `wss://${systemUrl}/websocket/`,
            changeOrigin: true,
            ws: true
        }
    ]
},

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question