This is the source code for a very simple chat example used for the Tutorial guide of the Socket.IO website.
You can run this example directly in your browser on:
This project forked from rauchg/chat-example
Basic chat example with Socket.IO
Home Page: https://socket.io
License: MIT License
This is the source code for a very simple chat example used for the Tutorial guide of the Socket.IO website.
You can run this example directly in your browser on:
Hi, I'll maybe use this code or snippet from this.
I can't see any license, so for now it means it's closed by default.
Is is free software ?
Will this run on socket.io v2.0 and later?
The example has Access-Control-Allow-Origin
problem.
I just edit index.html
var socket = io('http://localhost:3000');
Then the Chrome Console print like this
Failed to load http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1521768422803-4: The 'Access-Control-Allow-Origin' header has a value 'null' that is not equal to the supplied origin. Origin 'null' is therefore not allowed access.
Hi, I'm new to NodeJS .. I got to the last step and nothing happened. I looked over the code and noticed it was missing
var socket = io();
like the previous steps had
"Chat" works fine on "localhost", using 3 different browsers but on intranet, page is not"sending" or receiving. tried suggestion on #21 but did not fix
I am able to run the sample app downloaded from the following website: https://github.com/socketio/chat-example
However when I try to copy the index.js and index.html file to my existing project folder, the app can't seem to run. have also installed the dependencies:
May I missing other files/ dependencies?
In this project some i get is
First one is Two user are connected then it's not get alert or any type of other suggestion
Second one is when user click on the send button then the message not send or not show in any place like a nothing to see just realise message was send but which place send it's not see
SO i need some help please
any one do this then tell me i'm still waiting for a good developer
In this sample app, it hard codes a CDN reference to v1.2 of socket.io. When someone tries to implement this sample app and the current version of socket.io that they are using on their server is no longer 1.2, it does not work and it is difficult to figure out why. I would think the script tag should be <script src="/socket.io/socket.io.js"></script>
so that it always loads the right version of the client. For an example of how confused a socket.io newbie gets when this happens see this question/answer on stackoverflow.
Messages written before a user opens the page do not appear
I'm getting this error about the form addEventListener and I can't understand why.
I already tried to copy the code and still get the same error.
Uncaught TypeError: form.addEventListener is not a function
at index.js:10:6
`var socket = io();
var input = document.getElementById('input');
var roomInput = document.getElementById('room-input');
var messages = document.getElementById('messages');
var form = document.getElementById('form');
form.addEventListener('submit', function(e) {
e.preventDefault();
if (input.value) {
socket.emit('chat message', input.value);
input.value = '';
}
});
socket.on('chat message', function(msg){
var item = document.createElement('li');
item.textContent = msg;
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
});`
Can you help me, please?
Regards!
Pedro
when I download this demo; I can run it on chrome ;but not send message on ie or edge ; always say io is not defined
I got the socket js and jquery js file offline and ran my nodemon, but it doesn't work, but works if am using the socket js and jquery js file from cdn
I have few users who spam my chat. How can I determine their ip?
In the expamle, index.html
<script src="/socket.io/socket.io.js"></script>and then we can use io, but i don't understand how can we get this file in my localserver without any setting, the real url is 'http://127.0.0.1:3000/socket.io/socket.io.js'.
this problem has plagued me for a long time, thank U
if I using the step module with io.on('connection', function(){
});
it will have some problem, if the socket is lately start than client, the client will not receive other event from the socket server, it on have the connection.
please explain the problem, and how to fix it ?
If i replace
app.get('/', (req, res) => {
res.sendFile(new URL('./index.html', import.meta.url).pathname);
});
With
app.get("/", (req,res) => {
res.sendFile(__dirname + "/index.html");
});
And insert these codes
import { dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
Everything work fine !
added origins but got the same problem
var io = require('socket.io')(http, {origins: ':'});
This code is not working alrite....multiple socket.ids generated from the same tab. So sometimes client connects to himself or others connect to non existing client.
I wasted around 2 hours wondering why it's not working when I did everything like in this tutorial:
https://socket.io/get-started/chat/
I downloaded code from github. Copied everything from index.js - not working, everything from index.html - not working. Then I look to package.json - damn, that's some old one. Finally when I copied package.json and reinstalled it started working.
But I can't get it going with newest version because... tutorial is just shi**y, sorry
I followed the tutorial and I test it by opening 2 tabs to connect and it shows both are connected but when I send the message on one tab the other tab is not updated.
Hello, if i want close the terminal or navigate in other repertory, this close the server. How do for keep the server start ?? Thank you.
I'm using Firefox (108.0, MacOS 13.1).
When I run this project, the browser debugger shows there's some 500 responses from the server.
I added this code to the index.js
, and the server console logs:
io.engine.on("connection_error", (err) => {
// console.log(err.req); // the request object
console.log(err.code); // the error code, for example 1
console.log(err.message); // the error message, for example "Session ID unknown"
console.log(err.context); // some additional error context
});
I guess the problem is: the websocket connection was established successfully, but the polling connection failed.
So I tried to add a parameter to allow only polling:
const io = require('socket.io')(http, {
transports: ['polling']
});
Then, the connection never establishes successfully. The browser sends requests and gets 500 responses infinitely. The server also gets "Session ID unknown" infinitely.
Looks like a new connection starts to establish when the previous connection hasn't finished establishing.
I've tried the latest version (4.5.4) of "socket.io" and "socket.io-client", the behavior is the same.
And I found the polling connection can be successfully established in the latest version of Chrome, but failed in Edge as in Firefox.
How can I solve this?
Great example, however it seems my Microsoft Edge v 123.0.2420.1 is killing the script every time I start typing in Edge.
Edge is receiving other messages from Chrome and Firefox without problem.
C:\Users\ted\NODEDEV\WEBSOCKET3\index.js:46
callback();
^
TypeError: callback is not a function
at Socket. (C:\Users\ted\NODEDEV\WEBSOCKET3\index.js:46:7)
Node.js v18.18.2
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting node index.js
server running at http://localhost:3000
How can I resolve sending messages by Edge?
I have make a new variable call httpd and named it as var httpd = require('http');
and I use it to create and get the client request file, and now I want to emit a value when someone visit /greet
path it will emit to chat room so I add this before io.on
code
I add the following code
var httpd = require('http);
var path;
httpd.createServer(function (req, res) {
path = req.url;
};
if (path == "/greet") {
io.on ....
But the code doesn't emit a value to chatroom when someone visit /greet
What I'm missing?
Or we can't get req.url
in socket.io code?
What the title says. I have copied and slightly modified the code. It works as intended, but I can only connect from the machine it is running on (using localhost:3000
or 192.168.1.3:3000
). I have attempted to use port forwarding but it did not help the issue. I'd like to know how I can make the app accessible from another machine, and if it is possible to host the app on GH Pages.
Update socket.io to the latest version (4.1.2)
Hi,
The example, if you install express and socket.io via
npm install express socket.io
,doesn't work anymore, because the client index.html file references to an old socket.io version.
For me, the browser opens a lot of connection every second instead of keeping one.
If i change the script on the index.html to https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js it works again flawlessly.
BR
kekerooo
index.html, line 26:
var socket = io();
=> 'io' not defined
I tried copying and pasting the code and run the application. I checked the: " io.on('connection', function(socket){ " part and it works when a user connects. But the other part of " socket.on..... " doesn't seem to be working. When I enter texts in the form I get blank on the client page and the server is blank as well. I tried to test it by typing console.log to display something when "socket.on" gets fired, but nothing shows up. What's missing? Can anyone please help?
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.