chat-app-react-redux-saga-websockets icon indicating copy to clipboard operation
chat-app-react-redux-saga-websockets copied to clipboard

Websocket not connecting

Open semsion opened this issue 6 years ago • 3 comments

After cloning the repo, and assigning my IP and port (in .../src/sockets/index.js), cannot get rid of the below error:

uncaught at handleNewMessage at takeEvery at
Error: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.

semsion avatar Mar 26 '18 20:03 semsion

I fixed mine putting inside an express server, like this. Hopefully is useful for anyone having this problem:

const express = require('express');
const WebSocket = require('ws');

const app = express();

const server = require('http').createServer(app);
const wss = new WebSocket.Server({ server });
const users = [];

const broadcast = (data, ws) => {
	wss.clients.forEach((client) => {
		if (client.readyState === WebSocket.OPEN && client !== ws) {
			client.send(JSON.stringify(data));
		}
	});
};

wss.on('connection', (ws) => {
	let index;
	ws.on('message', (message) => {
		const data = JSON.parse(message);
		switch (data.type) {
			case 'ADD_USER': {
				index = users.length;
				users.push({ name: data.name, id: index + 1 });
				ws.send(
					JSON.stringify({
						type: 'USERS_LIST',
						users,
					}),
				);
				broadcast(
					{
						type: 'USERS_LIST',
						users,
					},
					ws,
				);
				break;
			}
			case 'ADD_MESSAGE': {
				broadcast(
					{
						type: 'ADD_MESSAGE',
						message: data.message,
						author: data.author,
					},
					ws,
				);
				break;
			}
			default:
				break;
		}
	});
	ws.on('close', () => {
		users.splice(index, 1);
		broadcast(
			{
				type: 'USERS_LIST',
				users,
			},
			ws,
		);
	});
});


server.listen(8989);

and in your client socket file you put const socket = new WebSocket('ws://localhost:8989');

Cheers!

hectorfaria avatar Mar 28 '18 15:03 hectorfaria

just sharing this in case it helps [re- similar questions in other 2 issues] -

i found at tail end of this video - https://www.youtube.com/watch?v=x_fHXt9V3zQ we need to cd into the /server directory and type node app.js to start the socket server [separate from npm start in the main directory /starting the app]

Ed.

ewellein avatar Apr 24 '18 23:04 ewellein

Hi, I am facing the same issue. I tried to solve it but now workig

any help will be appriciated...

index.js:2177 uncaught at handleNewMessage at takeEvery at
Error: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state. at http://localhost:3000/static/js/bundle.js:53689:21 at createTaskIterator (http://localhost:3000/static/js/bundle.js:43455:17) at runForkEffect (http://localhost:3000/static/js/bundle.js:43849:24) at runEffect (http://localhost:3000/static/js/bundle.js:43734:872) at next (http://localhost:3000/static/js/bundle.js:43615:9) at currCb (http://localhost:3000/static/js/bundle.js:43687:7) at takeCb (http://localhost:3000/static/js/bundle.js:43765:152) at Object.put (http://localhost:3000/static/js/bundle.js:42759:16) at http://localhost:3000/static/js/bundle.js:42847:10 at http://localhost:3000/static/js/bundle.js:42872:16 at exec (http://localhost:3000/static/js/bundle.js:44427:5) at flush (http://localhost:3000/static/js/bundle.js:44468:5) at asap (http://localhost:3000/static/js/bundle.js:44441:5) at Array. (http://localhost:3000/static/js/bundle.js:42871:71) at Object.emit (http://localhost:3000/static/js/bundle.js:42712:13) at http://localhost:3000/static/js/bundle.js:43272:21 at Object.dispatch (http://localhost:3000/static/js/bundle.js:53312:7) at onKeyPress (http://localhost:3000/static/js/bundle.js:53059:17) at HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:15703:14) at Object.invokeGuardedCallbackDev (http://localhost:3000/static/js/bundle.js:15741:16) at Object.invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:15790:29) at Object.invokeGuardedCallbackAndCatchFirstError (http://localhost:3000/static/js/bundle.js:15804:43) at executeDispatch (http://localhost:3000/static/js/bundle.js:16064:19) at executeDispatchesInOrder (http://localhost:3000/static/js/bundle.js:16086:5) at executeDispatchesAndRelease (http://localhost:3000/static/js/bundle.js:16184:5) at executeDispatchesAndReleaseTopLevel (http://localhost:3000/static/js/bundle.js:16195:10) at forEachAccumulated (http://localhost:3000/static/js/bundle.js:16165:8) at runEventsInBatch (http://localhost:3000/static/js/bundle.js:16326:5) at runExtractedEventsInBatch (http://localhost:3000/static/js/bundle.js:16335:3) at handleTopLevel (http://localhost:3000/static/js/bundle.js:20079:5) at batchedUpdates$1 (http://localhost:3000/static/js/bundle.js:32262:12) at batchedUpdates (http://localhost:3000/static/js/bundle.js:17734:12) at dispatchEvent (http://localhost:3000/static/js/bundle.js:20158:5) at interactiveUpdates$1 (http://localhost:3000/static/js/bundle.js:32317:12) at interactiveUpdates (http://localhost:3000/static/js/bundle.js:17753:10) at dispatchInteractiveEvent (http://localhost:3000/static/js/bundle.js:20135:3) stack_frame_overlay_proxy_console @ index.js:2177 log @ utils.js:225 end @ proc.js:337 abort @ proc.js:79 task.cont @ proc.js:92 end @ proc.js:346 abort @ proc.js:79 runForkEffect @ proc.js:553 runEffect @ proc.js:425 next @ proc.js:306 currCb @ proc.js:378 takeCb @ proc.js:456 put @ channel.js:73 (anonymous) @ channel.js:161 (anonymous) @ channel.js:186 exec @ scheduler.js:19 flush @ scheduler.js:60 asap @ scheduler.js:33 (anonymous) @ channel.js:185 emit @ channel.js:26 (anonymous) @ middleware.js:67 dispatch @ AddMessage.js:7 onKeyPress @ AddMessage.js:12 callCallback @ react-dom.development.js:100 invokeGuardedCallbackDev @ react-dom.development.js:138 invokeGuardedCallback @ react-dom.development.js:187 invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:201 executeDispatch @ react-dom.development.js:461 executeDispatchesInOrder @ react-dom.development.js:483 executeDispatchesAndRelease @ react-dom.development.js:581 executeDispatchesAndReleaseTopLevel @ react-dom.development.js:592 forEachAccumulated @ react-dom.development.js:562 runEventsInBatch @ react-dom.development.js:723 runExtractedEventsInBatch @ react-dom.development.js:732 handleTopLevel @ react-dom.development.js:4476 batchedUpdates$1 @ react-dom.development.js:16659 batchedUpdates @ react-dom.development.js:2131 dispatchEvent @ react-dom.development.js:4555 interactiveUpdates$1 @ react-dom.development.js:16714 interactiveUpdates @ react-dom.development.js:2150 dispatchInteractiveEvent @ react-dom.development.js:4532

index.js:8 WebSocket connection to 'ws://104.125.50.52:7778/' failed: Error during WebSocket handshake: Unexpected response code: 503

image

SushilMcm avatar Jul 11 '18 10:07 SushilMcm