Socket.IO

Service Worker vs Web Worker

戏子无情 提交于 2021-01-29 10:44:41
问题 I wanna implement push notification for my website: Consider we have a shopping site, whenever a user click on one product, the server should push notification to the owner of the product that user (for example John) wants to buy your shoe. (real-time). I have read some articles about web worker (like socket.io) and service workers, I conclude that service workers are the best choice. Am I true? How can I implement that push notification? 回答1: A Service Worker is a special kind of Web Workers

Why am I unable to send response object of Express through SocketIO?

ぃ、小莉子 提交于 2021-01-29 10:42:00
问题 I'm trying to send the response to an express request by sending it to a ws client and wait for its response. So I need to send the res object to the client (I couldn't find another way). Here is what I did: var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html'); }); io.on('connection', (socket) => { socket.on('res', (e) => { e.res.send(e.data) }) }) app.get('/endpoint',

React Hooks: state not updating when called inside Socket.io handler

孤街浪徒 提交于 2021-01-29 10:26:43
问题 const [questionIndex, setQuestionIndex] = useState(0); ... socket.on('next', () => { console.log('hey') setQuestionIndex(questionIndex + 1); }); ... useEffect(() => { console.log(questionIndex); }, [questionIndex]); I have a page that connects to a websocket using Socket.io. I am attempting to update the value of my questionIndex state variable when I receive a 'next' message from the socket. I seem to be receiving the message because 'hey' is printed, but questionIndex only updates after the

Socket.io and express app not connecting due to CORS error: “The value of the 'Access-Control-Allow-Origin' header must not be the wildcard '*'”

放肆的年华 提交于 2021-01-29 09:25:51
问题 I'm slowly losing my mind over a very stupid issue I'm having. I have a socket.io/express app uploaded to Digital Ocean as a Docker setup. To allow https, I am using caddy as part of my Docker setup to allow for automatic https. I've been trying to connect to this setup via my domain and from my local React app that lives on localhost:3000. But I am constantly getting the following error: Access to XMLHttpRequest at 'https://mediaserver.domain.dev/socket.io/?EIO=3&transport=polling&t=N5BXNK2'

How to use custom id as session id in Flask Socket IO

不羁岁月 提交于 2021-01-29 08:54:44
问题 I am using flask socket-io for a basic chat app. But I have a problem. When my device connect to server, flask give to device a session id. But after disconnect and again connect, session id changes. So, this device can't receive old room's messages. I thought if I'd do if I assign custom session id to device.Is it true? Or what is your suggestions? 回答1: The session ids are supposed to be unique, even when a client reconnects. There is no way for an application to choose these ids, they are

Reject a promise and stop timeout on socket-io event

删除回忆录丶 提交于 2021-01-29 07:18:40
问题 I'm currently using the following function to simulate awaiting (sleep) in my Node.js server script. function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } and I'm using it like below. When used in this way, executing the code below that line is paused until 20 seconds have been passed. await sleep(20000) Now I want to break this sleeping on a socket-io event , but I can't figure a correct way out to do that. I tried the following code, but it didn't break the sleep. /

How do I fix a server side socket.io error?

随声附和 提交于 2021-01-29 06:50:26
问题 I have the following lines of code in my index.js file: const express = require('express'); const app = express(); const server = require('http').Server(app); const io = require('socket.io').listen(server); When I start my server with: node --inspect server/index.js I get: TypeError: require(...).listen is not a function. Any and all help appreciated! 回答1: You need http server. The correct way to start a socket server is, const express = require("express"); const http = require("http"); const

Socket.io Client close with reason ping timeout

泄露秘密 提交于 2021-01-29 06:47:09
问题 server(node.js) io.on('connection', (socket) => { console.log('user connected'); socket.on('abc', data => { console.log(data); socket.emit('abc', {comment: 'server'}) }); socket.on('disconnect', () => { console.log('user disconnected'); }); socket.on('connect_error', (error) => { console.log(error); }); }); http.listen(3001, () => { console.log('listening on *:3001'); }); client(java) socket = IO.socket("http://gureuso.me"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override

Node JS how to limit maximum number of sockets

我只是一个虾纸丫 提交于 2021-01-29 05:13:34
问题 is it possibile to set the maximum number of sockets that my server can handle? Something like: maxSocket = 2 and from the 3 attempt of a socket to connect automatically refuse that connection? Thanks 回答1: In Node.js, you can set the maximum number of connections per origin. If maxSockets is set, the low-level HTTP client queues requests and assigns them to sockets as they become available. Ref URL from AWS JS SDK According to Node.js Doc, By default set to Infinity. Determines how many

Socket.io events not received (nginx reverse proxy, server to client)

馋奶兔 提交于 2021-01-29 04:20:51
问题 I have a nodejs socket.io app running under nginx reverse proxy. My nginx configuration is as follows: location ~ ^/(online|socket\.io) { proxy_pass http://localhost:8888; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } This seems to work, and my nodejs app picks up the connection fine. On the server side socket.io I have the following: io.of('/online').on('connection',