DEV Community

Ega Prasetya
Ega Prasetya

Posted on

10 3

Realtime Chat App - Golang

So in this chapter, we will to learn how to create chat app with Gorilla web socket, u should to download library with go get -u github.com/gorilla/websocket. So let`s do it.

BACKEND

Create file main.go, we will to create Socket Server first.

Alt Text

Next create 3 struct like this.

Alt Text

Next, create function main() its contents display template view index.html.

Alt Text

Next, create function handleIO()

Alt Text

Last, create function ejectConnection() & broadcastMessage().

Alt Text

FRONTEND

Create file index.html and follow this code

Alt Text

Next, we also to add style css.

Alt Text

Alt Text

And then last add also javascript.

Alt Text

Alt Text

TESTING

Alt Text

Full Source Code in here

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️